Question

I'm new to CakePHP but I've been though their FAQs and guides to no avail. This is so simple that I just must not be thinking straight:

How can I access a parameter sent through the URL within my view files?

Example: http://example.com/view/6

How would I take that parameter ("6") and cycle it through the controller to another view page?

If that's too complex for a quick answer, how can I reference the 6 within the view page itself? The 6 in this situation is the "Id" value in my database, and I need to set it as the "parent" -

Thanks

Was it helpful?

Solution

The URL, as you have it, will call the 6() method of your ViewController, which is not a valid method name. You may have to play with your routes to make that work.

If you don't want to configure your routes, you'll need the controller in the URL, like so:

http://example.com/thinger/view/6

which will call thingerControllerObject->view("6"). If you want "/view/" to go to a different method, edit the routes. See:

OTHER TIPS

Parameters can be retrieved like this

$this->params['pass']

Returns an array (numerically indexed) of URL parameters after the Action.

// URL: /posts/view/12/print/narrow
Array
(
    [0] => 12
    [1] => print
    [2] => narrow
)

To access the parameter in your view look in $this->params

Use the code below in the view file :

$url=Router::url($this->here, true);
$url_arr=explode("/",$url);

To see the content of $url been exploded simply print it using pr() as below :

pr($url_arr);

It will print associative array, thus you can access any particular number of parameter sent via url.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top