The prefix automagic in CakePHP routing

By now everyone knows that CakePHP version 1.2 has a new and very powerful routing mechanism, similarly the admin routing where URLs like http://blah.com/admin/profiles/add actually calls admin_add action in the profiles controller is also well known. Apart from nice looking URLs Admin routing allows a convenient method to namespace your access control – denying every admin_ action to anyone but those with Admin roles is almost a one liner in your app_controller.

But there are times when you need more than just admin routing, how about something like http://blah.com/user/profiles/edit and http://blah.com/user/profiles/changepassword ? If this could be routed to an action like user_add and user_changepassword wouldn’t it be great!! (eg: think ownership ACL checks)

The CakePHP devs thought that it indeed would be great and have provided a way to do exactly this. In your routes.php write

  1. Router::connect('/user/:controller/:action', array('prefix' => 'user'));

Yep! thats it… now go ahead and write actions with user_ prefix and they will be called appropriately when there is /user/ in the URL, there is an added bonus – since the html helper now uses the Router to construct URL your links will also automagically have the proper prefix. Thus, if in your view you write

  1. echo $html->link('Edit your profile', array('controller' => 'profiles', 'action' => 'user_edit'));

the resultant URL in href would be http://blah.com/user/profiles/edit

And yes again you can add as many prefixed routes as you want – so you can very easily have stuff like moderator, editor, manager in the URL with corresponding appropriate actions.

Go! Go crazy ;)

About Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

8 Responses to The prefix automagic in CakePHP routing

  1. mbavio April 4, 2008 at 5:45 pm #

    Wow… This is huge! I amaging myself doint a lot of things with that. Thanks for this tip!

  2. Jonathan Snook April 6, 2008 at 9:09 am #

    I’ve added this info to the documentation as Prefix Routing. Anytime you see something like this, be sure to add it to the documentation.

  3. Tarique Sani April 6, 2008 at 9:29 am #

    Thanks Jonathan for updating the cookbook – I intended to but “road to hell is paved…” and all those invalid excuses ;)

    Will be faster next time

  4. snowdog April 6, 2008 at 7:40 pm #

    Thanks. I was playing with configuring something like that in routes.php, didn’t know that it is so easy.

  5. zahid September 16, 2008 at 10:23 am #

    It’s great very helpful…

  6. mark January 25, 2009 at 6:22 am #

    strange.. this is not working somehow – with the current 1.2 stable (for me anyway)

    dont know what is wrong
    i have tried

    $html->link(‘useredit’, array(‘controller’=>’tests’,'action’=>’test’,'prefix’=>Configure::read(‘Routing.user’)));

    with the following in routes:

    Router::connect(‘/’. Configure::read(‘Routing.user’) .’/:controller/:action/*’, array(‘prefix’ => Configure::read(‘Routing.user’), Configure::read(‘Routing.user’) => 1));

    Configure::read(‘Routing.user’) =>>>> ‘user’;

    [i tried it hardcoded as well]

    what happens is, that the prefix is not attached to the link
    it is just
    http://…/tests/test/
    instead of
    http://…/user/tests/test/

    anyone else having some trouble on this matter lately?

Trackbacks/Pingbacks

  1. SaniSoft Blog: The prefix automagic in CakePHP routing | Development Blog With Code Updates : Developercast.com - April 9, 2008

    [...] the SaniSoft blog, Tarique Sani talks briefly about some of the prefix “automagic” that’s already built in to the CakePHP [...]

  2. Promet CakePHP» Blog Archive » Tip: use CakePHP Route::url() for your URLs - July 11, 2008

    [...] first 3 parameters. The latter can be written in any order as you like. I also added support for prefix routing such as admin_*. So you can write: url ( ‘controller’, ‘admin_action’, ‘plugin_name’, [...]

Leave a Reply