Fancy routing examples with CakePHP 1.2

Apr 15 2008

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.

h3. 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
  1.  
  2. Router::connect('/moderator/comments/:action', array('controller' => 'comments',
  3.                                                      'admin' => true));
  4.  

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
  1. Router::connect('/posts/feed', array('controller' => 'posts',
  2.                                      'action' => 'index',
  3.                                      'url' => array('ext' => 'rss') ));

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 expressions

Show Plain Text
  1. //Get ID's that are numeric and 1-3 characters in length
  2.  
  3. Router::connect('/posts/view/:id', array(), array('id' => '[0-9]{1,3}' ));
  4.  
  5. //Feed animals only if they are a bat, cat, or rat
  6. Router::connect('/animals/feed/:type',
  7.                     array('controller' => 'pets',
  8.                           'action' => 'feed'),
  9.                     array('type' => '[r|b|c]at')
  10.                 );

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/rat, /animals/feed/bat or /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.


Comments

There are no Comments, Be the First!


Have Something to say?

*
* (Never published, I promise)
* You can use Textile markup, but be reasonable

Recent Artwork

  • Shuriken 2
  • Clumsy Penguins
  • Balloon Animals
  • Metal in the air! (vertical)
  • Cleavers
  • Waiting