0

我需要一些帮助来为我的产品目录构建路线。我想要这样的网址:

/产品/电子产品/14

/产品/电子产品/计算机

/产品/电子产品/电脑/笔记本电脑/4

url 中的最后一个数字显示当前列表页码。

4

2 回答 2

3

我认为您需要定义自己的自定义路线(我更喜欢正则表达式,因为它的速度)。

我假设您有 3 个级别的类别 - 如果您需要更多,请编写一个循环来为您创建路线。根据需要修改控制器和操作。我假设页面参数是必需的 - 如果不修改正则表达式。

$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'
    )
);
于 2010-11-09T23:10:37.693 回答
1

您将不得不添加多条路线,例如

$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+'
    )
));

您可能希望对每个路由使用不同的控制器操作,这取决于您实际处理数据的方式。

请注意,这完全未经测试,只是我目前最好的猜测(现在在 .NET 中工作,甚至无法模拟它)

于 2010-11-09T23:10:39.160 回答