Cakephp router and prefixes

04/12/2008

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));

Links
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′

Pagination
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)));

Forms
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.

Bookmark and Share
Filed under: Cakephp,Webdevelopment
  • http://chez.egloos.com chez

    Thank you so much.
    I found this article after 2hour googling by ‘cakephp paginator prefix’

  • http://nearlybearded.com Ben

    Same deal as with chez, there doesn’t seem to be any relevant documentation on using custom prefix routing with pagination in CakePHP. Thanks. By the way, the third piece of code under “Pagination” is missing the third closing parenthesis.

  • Pingback: CakePHP router and custom prefixes « Myles Kadusale’s Blog

  • Brett

    *Bookmarked*… thanks so much, I’ve been wrestling with custom prefixes for a week now!

  • http://alexandersitedesign.com Matt Alexander

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

  • http://alexandersitedesign.com Matt Alexander

    THanks for the great code! couldn’t find it anywhere else.

  • http://www.pagebakers.nl Eelco Wiersma

    Thanks for noting Matt!

  • http://www.dellbrasil.net Dejanira

    i found your blog when i was looking for a different sort of information but i was very happy and glad to read through your blog. the information available here is great.http://www.dellbrasil.net