RESTful routes and prefixes with CakePHP

Published on and tagged with cakephp  configuration  tip

Recently, I experimented a bit with RESTful routing in CakePHP. I wanted to have REST urls with a prefix. For example “/example/posts”, with “/example” as prefix. And so I added the following snippet to app/config/routes.php:

Router::mapResources('Posts', array('prefix' => '/example'));

Unfortunately, it doesn’t work, and you get a “Controller not found” error when requesting “/example/posts”, because CakePHP looks for an Example controller instead of a Posts controller. The same error occurs when I remove the leading slash from the prefix, like it is done with “normal” prefix routing:

Router::mapResources('Posts', array('prefix' => 'example'));

At this point I went to the source and found the reason, why it didn’t work: the prefix simply gets concatenated with the underscored controller name while Router::mapResources() generates the necessary routes for the resource. And this means, the prefix must contain a leading and a trailing slash:

Router::mapResources('Posts', array('prefix' => '/example/'));

Not really intuitive ;-)

3 comments baked

  • Brendon Kozlowski

    I know you and CakePHP’s trac have a varied history, but have you checked to see if 1.3 has the same outcome, and thought of possibly creating an enhancement request?

    I would imagine that `/example`, `/example/`, `example/`, and `example` should all work the same, and that the only forward slashes that would be necessary would be in multi-level prefix path…not that I could think of a reason where someone would want to invoke a multi-level prefix path though…

    I can see why the enhancement request might be taken to heart (it helps make development easier by not having to know ins-and-outs of exactly how to set something up), but on the other side, CakePHP is about convention over configuration – even if this is a configuration, it follows a convention. It can’t hurt to try, I suppose. ;)

    Thanks for documenting this, Daniel.

  • Martin Westin

    Nice tip. That is exactly the kind of thing that can keep you guessing for an hour before going through the source. Thanks for possible saving me some time in the near future. I was just looking at Cake’s REST chapter today.

    There might be a point to have the prefix just concatenate. It lets you create prefixes like example_posts. I’m not sure I would use that kind of url but…

  • cakebaker

    @Brendon, Martin: Thanks for your comments!

    @Brendon: Yes, I checked the 1.3 branch, but the behavior there is the same. And so opened an enhancement ticket for it.

    Personally I think only “/example” should be supported, because all “normal” routes start with a slash and with a prefix you simply put something in front of those “normal” routes.

    @Martin: Good point. I think the term “prefix” is ambiguous in this case, its meaning can be a path prefix (that’s how I interpreted it), or as a name prefix like in your example. So maybe there should be two different “prefix” options…

© daniel hofstetter. Licensed under a Creative Commons License