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 this entry
You’re currently reading “ The prefix automagic in CakePHP routing ,” an entry on SANIsoft – PHP for E Biz
- Published:
- 4.4.08 / 3:07pm
- Category:
- CakePHP
- Author:
- Tarique Sani
8 Comments
Jump to comment form | comments rss | trackback uri