views:

67

answers:

1

Hi, I need to do some special routing in cake, but can't for the life of me figure it out.

I have a shop controller at /shop, the format of the url will be:

/shop/:category/:sub_category/:product_slug

In the routing I need to send each part of the url to a different action, for example if the url was just /shop/cakes it would go to the category action of shop.

However if the url was /shop/cakes/macaroons or /shop/cakes/fairy it would go to the sub category action on the shop controller.

And the the same again for /shop/cakes/macaroons/pistachio would go to the product action on the shop controller.

How would I go about this in the routing? Something starting with

Router::connect('/shop/:category/:sub_category/:product_slug' ...

Or am I way off the mark? Thanks.

+1  A: 

You'll need three routes, in this order:

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'
 *     )
 */
Daniel Wright