CakePHP Routes


The documentation covering routes in CakePHP is a bit confusing, and doesn’t quite cover how you can you use paths in the URL as method parameters. I’ll cover how I used them in a recent app in this short article.

Routing in CakePHP is similar to using Apache’s mod_rewrite to give you pretty URLS, for example, mapping:

   http://server/item/1 to http://server/items.php?id=1

They are used in Cake so that you don’t require mod_rewrite/Apache to achieve such cleaned-up URLS.

Cake’s route documentation (look under Section 3)
http://manual.cakephp.org/chapter/configuration

In my application I wanted to get rid of the controller/action convention and rather have URLs like http://server/about and http://server/contact that are both served by one controller called Pages

My route file looks like this:

 1   $Route->connect('/', array('controller' => 'pages'));
 2   $Route->connect('/:action/*', array('controller' => 'pages',
      'action' => ':action'));

Line 1 says that any request for the root document should be handled by the Pages controller, and since no action was specified, the method will default to index().

The second line catches one or more path parameters and assigns the first one to :action, and the rest are caught by the asterisk (*) and passed along to the controller.

Example

   http://server/about/cars/ford
   :action => 'about'
   parameters => 'cars', 'ford'

We then assign this route to the Pages controller, and to the action that was specified as the first path variable.

Inside pages_controller.php, we have something like this:

   function about($page = 'default', $type = null) {
      //code dealing with parameters
      //error checking etc.
      $this->render($chosen_page);
   }

In our example above, $page will equal ‘cars’ and $type will equal ‘ford’.

In this way you can have one controller serving a variety of different pages, out’ve one action - instead of the usual one action per page convention.

Share this post
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
  • Facebook
  • muti
  • Mixx
  • Google
  • laaik.it

This entry was posted on Wednesday, May 9th, 2007 at 4:05 pm and is filed under General. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

11 Responses to “CakePHP Routes”

  1. cbmeeks said this on

    Man, thanks.

    I really like that system.

    I think I will use something similar in my Cash Flow system.

    http://www.signaldev.com

  2. Nate said this on

    Forgive me if this is a simple question, but how would I go about database interaction when doing this? I would like to pull the info on the index/about/contact pages from the database, but I don’t know how to give them a model when using this technique. The pages controller says it has no model, so how would I go about this? Thanks.

  3. Nate said this on

    Looks like I answered my own question. It appears you can give the pages_controller a model and then do your logic in the respective methods.

    I guess my question now is: Is this the right way to do this? Seems kind of funky, but it does work.

  4. michael said this on

    Yeh, make a Page model - you could then use the `Page` table in your database and store pages (id, name, content) kind’ve thing.

    Then,
    $content = $this->Page->findByName(’about’);

  5. Eric said this on

    Are you sure
    ‘action’ => ‘:action’

    is what you want? Isn’t that element for the default action?

  6. joren said this on

    thanks, you have saved me :P

    works great, although the order of the routes are very important, so try them out

  7. Christher said this on

    This was nice. I have looked for a way todo this.

    It not nice ti see the controller name in the url.

  8. Christher said this on

    This works fine with one controller.

    How should I do to make this work with two controllers?

  9. Tamatha Pugh said this on

    k39qjd5n39kkedj8

  10. V-TEK Channel: » A call for cakephp favourite links said this on

    [...] http://webexpose.org/2007/05/09/cakephp-routes/ - Cake routes explained easily [...]

  11. Matt said this on

    This is actually a very nice way of routing but I have to say I think that the pages_controller can become quite big.

    Also an issue is that your views are always in /views/pages which is not nice also.

    I’m thinking about a controller file that includes the right real controller as you normally would use in the full url,but than rewritten using this specific controller. In this case you also have views/[controllername] for your views, which is nice.

    This can be done by routes, but you will end up with to many routes that I’m looking for a decent way like above.

    Has anyone some idea’s as I’m brainstorming and every time come back to all these routes which I don’t want to have :)

    Thanks.

Leave a Reply