Hello Mark,
The implementation of slug on the routes.php reminded me of ruby mixins. This feature is cool, will ease lots of pain of the developers.

Rajib Ahmed on 1/28/10

Thanks for this article, it is something I wanted to test when I saw the changes in 1.3. It looks quite interesting!

Just a remark as I’m not sure I understand it well: in the last example L12, why is the condition still here? Is it a line you forgot to delete or is there a reason?

Pierre Martin on 1/28/10

Hi Mark,

Great example!

Say if I wanted to do something a bit different, like have all requests go through a find in the DB and on match, go to a specific plugin/controller/action combo, matching the url – do you have an idea for a clean implementation of that?

Currently I have a custom class that searches a model “Sitetem” which have url, plugin, controller and action fields, if there’s a match I do something like :

$result = $this->SiteItem->findByUrl($url);
Router::connect($url, array(‘controller’ => $result[‘SiteItem’][‘controller’], ‘action …. etc

It works, but not the cleanest method I guess.

Kim Biesbjerg on 1/28/10

This is really cool, thanks for the Update!
Just came across the same problem, doing it in the controller at the moment, meh…

@Kim: Guess you could just set $params[‘controller’] to the value you got from your query and then return $params, as in the example.

leo on 1/28/10

Pierre: the extra conditions were a mistake, and have been removed, thanks :)

Kim: If the other url parameters are stored in the db, you could just retrieve them, modify the request params and return the modified results.

mark story on 1/28/10

Interesting. I hadn’t gotten a chance to look into the new 1.3 features, but I think now I will have to. Thanks!

Dave on 2/6/10

Great feature, i’m trying to make some free time for try new features.

Thanks for nice explained article

Furkan Tunalı on 2/8/10

Greate tutorial! I will try it with new cake experience.

sophy on 2/10/10

There’s one small problem that could be a big performance hit. A new class is created every time even if the same route class is used.

For example, if I have routes like this (pseudo):

/:category => CategoryRoute
/:category/:slug => CategoryRoute
/:language/:category => CategoryRoute
/:language/:category/:slug => CategoryRoute

In each of those I’m going to be checking the category and sometimes the available languages for the site. Since there’s a new class created each time if I open that category list from a cache file or persist file, I’ve got to reopen and unserialize the data for every route check.

Barry on 2/24/10

Barry: An object is created for every route connected regardless of whether or not you define a routeClass. While checking a database could be slow, checking a cache system like apc or memcache is generally quite fast. You could even do things like implementing simple memory storage through a static class property, this would save you access time, but consume more memory. I haven’t done any tests, but I’m guessing that reading from memcache is not an overly expensive process :)

mark story on 2/24/10

Looks excellent and didn’t know of this new addition!

Good work

zeen on 4/2/10

What’s the best way to make the Router go to CONTROLLER_PREFIX.php instead of CONTROLLER.php whenever a PREFIX is used ?

I like to keep my PREFIX actions packed in individual files rather than a single file.

Valentin on 4/16/10

Someone just pointed this to me when I explained what I was trying to do. It looks like just what I need.

For a webshop I’m building I have the following URL structure: /url1/url2/url3/url4

Possible route matches for this URL structure are:
url1 = static page, url2 = subpage
url1 = category, url2 = product, url3 = product option
url1 = category, url2 = subcategory, url3 = product, url4 = product option
url1 = brand, url2 = product, url3 = product option

I’m already making sure URLs are always unique, and since they have the same structure it’s impossible to use regular expressions to find out what each part is referring to. I was planning to build an interpreter function to check that, but from the looks of things I can use a Route class for that when 1.3.0 gets released.

Thanks for making this functionality part of Cake!

Rick on 4/23/10

I dont know if i dont understand the propossal of this very well.. but you dont should pass at least the id of the post? what you earn passing the same think that the standard route pass?

I know this may can be a goffy cuestion but.. can you please explain me a little what this can be use for?

Manolo on 4/25/10

Thanks a lot!
Really useful for my project!

Munkey on 4/30/10

Hi. Looks great but how do I pass the ID? For me it only routes to the controller and action, not the id.

Erik on 6/10/10

Got it to work by adding a array(‘pass’ => array(‘slug’)) in the route and checking slug in the controller instead of ID. Don’t know if that was correct.

Erik on 6/10/10

Ok, I figured that was probably not correct. So how do I pass the slug? As previous it only goes to the controller and action but without passing the slug to the controller.

Erik on 6/10/10

LMAO, what a life saver! dude, I was hacking my WWW.ROOT.‘index.php’ file like so:

$Dispatcher = new Dispatcher();
$url = (empty($_GET[‘url’])) ? ‘’ : $_GET[‘url’];
$url = ClassRegistry::init(‘Contents.Content’)->translateUri($url);
$Dispatcher->dispatch($url);

Puts my hack to shame. You are a genius! Much cleaner and I don’t fight with route::connect. I’ll be replacing my hack with this.

Angel S. Moreno on 6/24/10

I mentioned this on http://cakeqs.org/questions/view/how_do_i_create_routes_from_database

Angel S. Moreno on 6/30/10