Routing in CakePHP is quite flexible in how you can route your urls to your controllers and actions. Offering both variable replacement and regex routing. You can route almost any parameter that is set by dispatcher and more. So lets try a few of these.
Replacing the admin routing
There are times when you don’t want the admin prefix. Now you could just change the admin prefix in your core.php. But then you need to refactor all your admin methods. Or you could set some fancy routing. Perhaps you want some admin prefixed methods and some non-admin prefixed methods. Say we had a few moderation actions that we didn’t necessarily want to have admin in front of it.Show Plain Text
- Router::connect('/moderator/comments/:action', array('controller' => 'comments',
- 'admin' => true));
This would connect any comments action to the comments controller with admin routing activated.
Turn on Extension based routing without using an extension.
As covered in a previous article RssHelper . You can turn on webserivce routing with out the extensions. Freeing your URL’s from file extensions.Show Plain Text
This route is an example of controlling not so normal route information. Basically it seems that you can set any parameter that shows up in your controllers
$this->params through a route. By specifying the a structure similar to how it comes out in the
$this->params in a route it will be set in your controller.
Use a Regular Expression
Regular expressions are super nifty and allow you to create advanced patterns that you can match requests against. If you want to learn more about Regular expressionsShow Plain Text
Both of these routes give examples of how to use regular expressions with routing. The optional 3rd parameter of connect, allows you to define simple regular expresssion (no capture groups) to define what is a valid parameter. In the first example we define ID as a number that is between 1-3 characters. The second route will match
/animals/feed/cat but not
/animals/feed/cow. No food for the cows today.
So that is just a few examples of how you can do fancy routing in CakePHP.