Zend Framework 产品目录路线

发布于 2024-10-01 02:21:28 字数 157 浏览 4 评论 0原文

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

/产品/电子产品/14

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

/产品/电子产品/计算机/笔记本电脑/4

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

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

/products/electronics/14

/products/electronics/computers

/products/electronics/computers/laptops/4

Last numbers in urls shows current listing page number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-10-08 02:21:28

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

我假设您有 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'
    )
);

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'
    )
);
旧话新听 2024-10-08 02:21:28

您必须添加多个路由,例如

$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 中工作,甚至无法模拟它)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文