Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email: hello@sanisoft.com

Blog

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

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

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 the Author

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 comments

  1. Pingback: SaniSoft Blog: The prefix automagic in CakePHP routing | Development Blog With Code Updates : Developercast.com

  2. Pingback: Promet CakePHP» Blog Archive » Tip: use CakePHP Route::url() for your URLs

  3. 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?

Leave a Reply