Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 584
scrot Avatar asked Oct 24 '08 19:10

scrot


People also ask

How can I get URL parameters in cakephp?

Cakephp get url parameters Syntax – // Get the Passed arguments $this->request->pass; $this->request['pass']; $this->request->params['pass']; The above syntax will give you the arguments of url passed along with controller.

How can I get post data in cakephp?

You can retrieve post data as Array. $post_data= $this->request->data; You can retrieve post data for particular key.

What is routing in cakephp?

Routing provides you tools that map URLs to controller actions. By defining routes, you can separate how your application is implemented from how its URLs are structured. Routing in CakePHP also encompasses the idea of reverse routing, where an array of parameters can be transformed into a URL string.


3 Answers

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
)
like image 191
Nikolay Ruban Avatar answered Nov 23 '22 18:11

Nikolay Ruban


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

like image 36
neilcrookes Avatar answered Nov 23 '22 17:11

neilcrookes


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:

  • Cake controllers
  • Cake routes
like image 38
Lucas Oman Avatar answered Nov 23 '22 17:11

Lucas Oman