开发者

Cakephp Nested Routes

开发者 https://www.devze.com 2023-02-23 02:33 出处:网络
I\'ve got a controller called Items and another one called Categories. Items may belong to different Categories.

I've got a controller called Items and another one called Categories. Items may belong to different Categories.

The routes that I have are

/items
/items/:item_id
/items/categories
/items/categories/:category_id

And this is my routes.php

Router::connect('/items', array('controller' => 'items', 开发者_StackOverflow'action' => 'index');
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));

The problem is that the third route fails as it routes to the Items controller which it shouldn't.

How can I fix this problem?


be careful on the order of your routes... cake will match to the first occurrence, so try using your routes like this:

Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));

good luck


in you config/routes.php

put a line

Router::connect('/items/categories', array('controller' => 'categories',
 'action' => 'index'));

or something similar.

it will redirect all you /items/categories to the index action of categories controller.

for admin pages (actions that use admin prefix):

Router::connect('/items/categories', array('controller' => 'categories',
 'action' => 'index','admin'=>true));
0

精彩评论

暂无评论...
验证码 换一张
取 消