Reputation: 3690
I'm trying to build a sort of Wordpress-esque CMS system to a project I'm building. I want the user to be able to create pages on the fly, and for them to appear in certain areas of the website.
I have made something similar in Symfony2, where the controller grabs a specific variable from the URL (as definded in the route.yml file, usually $id etc) and I then use the variable in the controller to display whatever content it relates to in the database.
However, I'm not used to CakePHP 2.0, and struggling to find what I need. I know it's possible, but I don't know the best way to achieve it. Especially as CakePHP uses a different routes file than Symfony.
How would I grab a variable from the URL and pass it for use inside a controller?
Upvotes: 0
Views: 1526
Reputation: 111249
By variable do you mean GET query string parameters, like in /foo?key=value
? You can access them in the controller through the request object: $this->request->query['key']
.
If you are looking for something more integrated you can use CakePHP's default routes or make your own.
The default routes work with URLs like /controller/action/param1/param2
and pass the parameters to the action by position. For instance /posts/view/521
maps to a call to view(521)
in PostsController
, and /posts/byMonth/2012/02
maps to a call to byMonth("2012","02")
.
You can also use named parameters and the URLs look like /controller/action/key1:value1/key2:value2
. In controller actions you would read them with $this->params['named']['key1']
.
With custom routes you can make your URLs anything you want. You're not forced to the /controller/action pattern; you can make /archives/2012-02
map to PostsController::byMonth(2012,2)
, or have /512-post-title
map to PostsController::view(512)
.
Typically you would start out with the default routes and add custom routes when you decide you need them. You can read all about the default and custom routes in http://book.cakephp.org/2.0/en/development/routing.html
Upvotes: 3
Reputation: 1831
Short answer: it depends.
If you're looking to pass parameters to a function, you don't need to mess with routes at all; every non-named URL path segment is processed in order and handed to the action as method parameters (so /controller/action/1234
passes "1234" as the first parameter to the action
method in the controller
controller class).
Named parameters allow you to pass parameters anywhere in the URL string, and to make them optional. The form is just key:value and they're accessed via $this->params['named']
in the controller.
The last option is prefix routing. The best place to get to speed on that is naturally the CakePHP Cookbook, but the general gist is that in a route definition, you can name a path component in the URL by prefixing it with a colon, identically to how the default routes show :controller
, :plugin
and :action
. You can define any name you like, even optionally applying regex pattern requirements and then access it through $this->params['variablename']
in the controller.
Upvotes: 1