Posted on 17/5/06 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
We all know that cakephp wants us to structure all our urls in the "/controller/action/params" (or "/plugin/controller/action/params") way, but sometimes you want domain.com/product or something else that would break that structure. Now you can use routes for that, but what is, if you want to create those aliases dynamically? You could dynamically create a new routes.php, but that's a rather messy approach. A better one is to do some URL rewriting yourself. Here is the solution I came up with:
Where to download
I put the code used as a snipped on CakeForge: Get it here!
How to implement
I created a little php script to put in the vendor folder. Then you can edit your routes.php and rewrite controllerMissing errors to your own Controller.
CakeUrlAlias_Activate($from_url, 'pages', 'display');
The example above would rewrite all controllerMissing errors to your pages controller. Inside this controller you would check if there is a content matching the requested page and if not, trigger the missingController error:
return $this->cakeError('missingController', array(array('className' => Inflector::camelize($controllerName."Controller"),
'webroot' => $this->webroot,
'url' => $this->params['url']['url'],
'base' => $this->base)));
(when you do this $this->triggerMissingController() you need to pass the name of the $site or whatever that couldn't be found).
I hope this should help some people who are particular about the way their urls should look ; ). It's mostly ment for Search Engine Optimization since Google & Co like keyword density, and a controller name in between domain and page can be bad sometimes.
--Felix Geisendörfer aka the_undefined
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.