Cakephp router and prefixes

Cakephp has been supporting admin routes for a long time. It’s also possible to use custom prefixes next to your admin routes for extra flexibility. However you will probably face some problems when you start using prefix routes. I’ll show you how to handle some of them.

First of all let’s create our prefix route

Router::connect('/manager/:controller/:action/*', array('prefix' => 'manager', 'manager' => true));

Creating links to your prefixed actions is easy

echo $html->link('Edit user', array('controller' => 'users', 'action' => 'edit', 'manager' => true, 1));

This will create a link to : ‘/manager/users/edit/1′
When using pagination in your prefixed actions, you’ll notice the paginator helper doesn’t output the correct url’s by default. Here’s how to fix it:

$paginator->options(array('url' => array('manager' => true)));

The form helper doesn’t detect the prefix automatically also.. here’s how to solve it

echo $form->create('User', array('url' => $html->url(array('pb' => true, $this->data['User']['id']))));

Unfortunately the Router class doesn’t detect the prefixes automatically, like it does with admin routes. Which results in a lot of extra code and work when you decide to use prefixes. Hopefully this will be changed in later releases.