Question

The CMS i'm developing using Cakephp 2.0 has two main Controllers:

  • Pages
  • Categories

I'm trying to set the route.php to have the following behavior:

If the user request a Page, the URL should be something like:

http://www.something.com/pages-alias/article-name/id/whatever

If the user address a Category, the URL should be something like:

http://www.something.com/categories-alias/category-name/id/whatever

Please notice that following categories and pages i've used "alias". To clarify with an example, the URLs for a website of a local restaurant will be:

http://www.something.com/course/wild-boar/68/2013-07-18

Where "course" will substitute "page". And

http://www.something.com/menu/valentine-day/8/2014-01-30

Where "menu" will substitute "category".

The View should not be explicited in the URL nor the Routing rules.

Both the cases will have the view automatically choosen by the controller after some internal check (having subcategory, having only one page or more pages, and so on) so that will be overridden by the controller.

I've got some clues about the use of sort-of "alias" to build the routing rules but unfortunately the documentation was not clear enough to me about how to manage the object to create my own custom route.

So, can someone try to explain it with some example different from the ones available in the CakePhP 2.x documentation?

Thanks in advance to anyone that can be helpful. For reference i'll paste here the links i've already read:

Was it helpful?

Solution

That is what you want probably:

 Router::connect(
    '/:category_alias/:category_name/:id/:whatever', 
    array('controller' => 'Article', 'action' => 'view'), 
    array('pass' => array('category_alias','category_name','id','whatever'), 
    'id' => '[0-9]+')
 );

ofc you can delete this validator for id.. or add more validators :)

Then you can use function in ArticleController.php

public function view($category_alias, $category_name, $id, $whatever) {}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top