开发者

Zend Framework product catalog routes

I need some help to build routes for my product catalog. I whant to have urls something like this:

/products/electronics/14

/products/el开发者_JS百科ectronics/computers

/products/electronics/computers/laptops/4

Last numbers in urls shows current listing page number.


I think you need to define your own custom routes (i prefer regex for that because of it's speed).

I assume that you have 3 levels of categories - if you need more write a loop to create routes for you. Modify controllers and actions for your need. I assumed that page param is required - if not modify regexps.

$router = Zend_Controller_Front::getInstance()->getRouter();

//main category route
$router->addRoute(
    'category_level_0',
    new Zend_Controller_Router_Route_Regex(
        '/products/(\w+)/(\d+)',
        array(
            'controller' => 'product',
            'action'     => 'category',
            'module'     => 'default'
        ),
        array(
            1 => 'category_name',
            2 => 'page_nr'
        ),
        '/products/%s/%d'
    )
);

//sub category route
$router->addRoute(
    'category_level_1',
    new Zend_Controller_Router_Route_Regex(
        '/products/(\w+)/(\w+)/(\d+)',
        array(
            'controller' => 'product',
            'action'     => 'category',
            'module'     => 'default'
        ),
        array(
            1 => 'category_name',
            2 => 'sub_category_name'
            3 => 'page_nr'
        ),
        '/products/%s/%s/%d'
    )
);

//sub sub category route :)
$router->addRoute(
    'category_level_2',
    new Zend_Controller_Router_Route_Regex(
        '/products/(\w+)/(\w+)/(\w+)/(\d+)',
        array(
            'controller' => 'product',
            'action'     => 'category',
            'module'     => 'default'
        ),
        array(
            1 => 'category_name',
            2 => 'sub_category_name'
            3 => 'sub_sub_category_name'
            4 => 'page_nr'
        ),
        '/products/%s/%s/%s/%d'
    )
);


You would have to add multiple routes, something like

$router->addRoute('level1cat', new Zend_Controller_Router_Route(
    'products/:cat1/:page',
    array(
        'controller' => 'product',
        'action'     => 'index',
        'page'       => 1
    ),
    array(
        'cat1' => '\w+',
        'page' => '\d+'
    )
));

$router->addRoute('level2cat', new Zend_Controller_Router_Route(
    'products/:cat1/:cat2/:page',
    array(
        'controller' => 'product',
        'action'     => 'index',
        'page'       => 1
    ),
    array(
        'cat1' => '\w+',
        'cat2' => '\w+',
        'page' => '\d+'
    )
));

$router->addRoute('level3cat', new Zend_Controller_Router_Route(
    'products/:cat1/:cat2/:cat3/:page',
    array(
        'controller' => 'product',
        'action'     => 'index',
        'page'       => 1
    ),
    array(
        'cat1' => '\w+',
        'cat2' => '\w+',
        'cat3' => '\w+',
        'page' => '\d+'
    )
));

You may want to use different controller actions per route, it's up to you how you actually handle the data.

Note, this is totally untested and is just my best guess at the moment (working in .NET right now, can't even mock it up)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜