Cakephp routing without id?

Is there a way to route the URLs in the cake without the id in the url?

So instead of www.mydomain.com/id/article-name I just want www.mydomain.com/article-name

I follow this. http://book.cakephp.org/view/543/Passing-parameters-to-action

+4
source share
2 answers

Sure. The only requirement for this is that there is enough unique information in the URL to bind the desired article. If /article-name is unique in your database, you can use it to search for the required entry.

In config / routes.php:

 // ... configure all normal routes first ... Router::connect('/*', array('controller' => 'articles', 'action' => 'view')); 

In the / articles _controller.php controllers:

 function view ($article_name) { $article = $this->Article->find('first', array( 'conditions' => array('Article.name' => $article_name) )); ... } 

Be careful not to name your products like anything that could legitimately appear in a URL, so you don't run into conflicts. Does the URL http://example.com/pages to product pages or to an array('controller' => 'pages', 'action' => 'index') ? To do this, you will also need to define your routes in routes.php so that all your controllers are available in the first place, and only the undefined rest will be transferred to your ArticlesController . Take a look at the third parameter, Routes::connect , which allows you to specify a RegEx filter that you could use for this purpose.

+6
source

You can do it:

 // In routes.php $rewrites = array(); $rewrites = am($rewrites, ClassRegistry::init('Article')->rewrites()); $rewrites = am($rewrites, ClassRegistry::init('AnotherModel')->rewrites()); $rewrites = am($rewrites, ClassRegistry::init('YetAnother')->rewrites()); foreach ($rewrites as $rewrite) { Router::connect($rewrite[0], $rewrite[1], $rewrite[2]); } 

With the deceze method, you can only catch everyone. In this method, you can define the entire stack if you want.

This method is a hack, though, since you are requesting a model from a configuration file.

0
source

All Articles