Cake PHP路由问题

时间:2010-04-28 13:49:48

标签: php ruby-on-rails cakephp routing url-routing

我需要在蛋糕中做一些特殊的路由,但不能为我的生活弄明白。

我在/shop有一个店铺控制器,网址格式为:

/shop/:category/:sub_category/:product_slug

在路由中,我需要将网址的每个部分发送到不同的操作,例如,如果网址只是/shop/cakes,则会转到商店的类别操作。

但是,如果网址为/shop/cakes/macaroons/shop/cakes/fairy,则会转到商店控制器上的子类别操作。

/shop/cakes/macaroons/pistachio的相同内容将转到商店控制器上的产品操作。

我如何在路由中解决这个问题?以

开头的东西
Router::connect('/shop/:category/:sub_category/:product_slug' ...

还是我离开了标记?谢谢。

1 个答案:

答案 0 :(得分:1)

您将需要三个路线,按此顺序:

Router::connect(
    '/shop/:category/:sub_category/:product_slug',
    array('controller'=>'shops','action'=>'product'),
    array('pass'=>array('product_slug'))
);
// Dispatches to ShopsController::product( $product_slug )
/*
 * Reverse route: 
 *     array(
 *         'controller'=>'shops','action'=>'product',
 *         'category'=>$some_category', 'sub_category'=>$some_sub_category
 *         'product_slug'=>$some_product_slug
 *     )
 */

Router::connect(
    '/shop/:category/:sub_category',
    array('controller'=>'shops','action'=>'subcategory'),
    array('pass'=>array('sub_category'))
);
// Dispatches to ShopsController::subcategory( $sub_category )
/*
 * Reverse route: 
 *     array(
 *         'controller'=>'shops','action'=>'product',
 *         'category'=>$some_category', 'sub_category'=>$some_sub_category
 *     )
 */

Router::connect(
    '/shop/:category',
    array('controller'=>'shops','action'=>'category'),
    array('pass'=>array('category'))
);
// Dispatches to ShopsController::category( $category )
/*
 * Reverse route: 
 *     array(
 *         'controller'=>'shops','action'=>'product',
 *         'category'=>$some_category'
 *     )
 */