Zend Framework Router 动态路由

发布于 2024-10-24 02:45:46 字数 2262 浏览 4 评论 0原文

我遇到了一个问题,但我似乎找不到一个好的解决方案来使其发挥作用。我必须在 Zend Framework 项目中创建一些动态路由。我将简短地解释我的问题是什么:

我需要有动态自定义路由来“扩展”默认路由(module/controller/action/params)。我正在从事的项目有几个合作伙伴,路线必须与这些合作伙伴合作。 为了存储合作伙伴,我创建了一个静态类,它看起来像这样。

<?php
    class App_Partner
    {
        static public $partners = array(
            array(
                'name' => 'partner1',
                'picture' => 'partner1.jpg'
            ),
            array(
                'name' => 'partner2',
                'picture' => 'partner2.jpg'
            ),
            array(
                'name' => 'partner3',
                'picture' => 'partner3.jpg'
            )
        );
        static public function routePartners() {
            $partners = array();

            foreach(self::$partners as $partner) {
                array_push($partners, strtolower($partner['name']));
            }
            $regex = '(' . implode('|', $partners) . ')';

            return $regex;
        }
    }

因此,App_Partner::routePartners() 返回一个类似 (partner1|partner2|partner3) 的字符串,我用它来创建正确的路线。我的目标是为每个合作伙伴为我在 Bootstrap 中设置的每条路线提供自定义路线。因此,如果我设置了一条路线 add-product.html ,我希望它适用于每个合作伙伴,如 partner1/add-product.htmlpartner2/add- Product.htmlpartner3/add-product.html。 此外,partner1/partner2/partner3 应路由到 default/index/index

事实上,我使用如下所示的路线使这件事发挥作用。

<?php
$routeProposal = new Zend_Controller_Router_Route_Regex(
    App_Partner::routePartners() . '?/?proposals.html',
    array(
        'module' => 'default',
        'controller' => 'proposal',
        'action' => 'index',
        'page' => 1
    ),
    array( 1 => 'partner'),
    "%s/proposals.html"
);
$router->addRoute('proposal', $routeProposal);

问题

如果我在请求 URI 中使用合作伙伴,上面的路由工作正常,但如果我不这样做,我会得到像 public//proposals.html 这样的双斜杠,因为上述路由中设置的反向路由为"%s/proposals.html"。我似乎无法找到一种方法来避免这种反向路由,因为我使用 url 视图助手 构建我的 url,如果未设置反向路由,我会收到一个异常说明。

我还需要在没有合作伙伴集的情况下工作的路线,这将是默认方式(add-product.htmlproposals.html 等)。

I bumped into a problem and I can't seem to find a good solution to make it work. I have to make some dynamic routes into a Zend Framework project. I'll explain shortly what my problem is:

I need to have dynamic custom routes that "extend" the default route (module/controller/action/params). The project I'm working for has several partners and the routes have to work with those.
To store the partners I've made a static class and it looks like this.

<?php
    class App_Partner
    {
        static public $partners = array(
            array(
                'name' => 'partner1',
                'picture' => 'partner1.jpg'
            ),
            array(
                'name' => 'partner2',
                'picture' => 'partner2.jpg'
            ),
            array(
                'name' => 'partner3',
                'picture' => 'partner3.jpg'
            )
        );
        static public function routePartners() {
            $partners = array();

            foreach(self::$partners as $partner) {
                array_push($partners, strtolower($partner['name']));
            }
            $regex = '(' . implode('|', $partners) . ')';

            return $regex;
        }
    }

So App_Partner::routePartners() return me a string like (partner1|partner2|partner3) which I use to create the right routes. My goal is to have the custom routes for each partner for every route I have set in the Bootstrap. So if I have a route add-product.html set I want it to work for each partner as partner1/add-product.html, partner2/add-product.html and partner3/add-product.html.
Also, partner1/, partner2/, partner3 should route to default/index/index.

In fact, I made this thing to work using routes like the one below.

<?php
$routeProposal = new Zend_Controller_Router_Route_Regex(
    App_Partner::routePartners() . '?/?proposals.html',
    array(
        'module' => 'default',
        'controller' => 'proposal',
        'action' => 'index',
        'page' => 1
    ),
    array( 1 => 'partner'),
    "%s/proposals.html"
);
$router->addRoute('proposal', $routeProposal);

The problem

The above route works fine if I use a partner in the request URI, but if I don't, I get double slashes like public//proposals.html because of the reverse route set in the route above to be "%s/proposals.html". I can't seem to find a way to avoid this reverse route because I build my urls using the url view helper and if the reverse route isn't set I get an exception stating this.

I also need the routes to work without a partner set, which will be the default way (add-product.html, proposals.html etc).

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

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

发布评论

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

评论(2

攒一口袋星星 2024-10-31 02:45:46

从您的描述来看,您似乎正在寻找 zend 路由器链,其中您的合作伙伴是可选链。

这是一个类似的问题,但使用主机名路由: Zend Framework:从路由获取子域参数。我对其进行了调整来解决您的问题,只需将以下内容放入 Bootstrap.php 中即可初始化路由:

protected function _initRoute()
{
    $this->bootstrap('FrontController');
    $router = $this->getResource('FrontController')->getRouter();

    // Default route
    $router->removeDefaultRoutes();
    $defaultRoute = new Zend_Controller_Router_Route(
                    ':controller/:action/*',
                    array(
                        'module' => 'default',
                        'controller' => 'index',
                        'action' => 'index',
                    )
    );
    $router->addRoute('default', $defaultRoute);

    $partnerRoute = new Zend_Controller_Router_Route(
        ':partner',
        array('partner' => 'none'),
        array('partner' => '^(partner1|partner2|partner3)

根据您的需要进行更改。在您的控制器中,只有在实际指定且有效的情况下,您才会获得合作伙伴参数的值(如果合作伙伴不存在,您将收到路由错误)...

) ); $router->addRoute('partner', $partnerRoute->chain($defaultRoute)); }

根据您的需要进行更改。在您的控制器中,只有在实际指定且有效的情况下,您才会获得合作伙伴参数的值(如果合作伙伴不存在,您将收到路由错误)...

From your description, it seems like you're looking for a zend router chain, where your partner is an optional chain.

Here's a similar question, but using a hostname route : Zend Framework: get subdomain parameter from route. I adapted it to solve your problem, just put the following in your Bootstrap.php to initialize the routing :

protected function _initRoute()
{
    $this->bootstrap('FrontController');
    $router = $this->getResource('FrontController')->getRouter();

    // Default route
    $router->removeDefaultRoutes();
    $defaultRoute = new Zend_Controller_Router_Route(
                    ':controller/:action/*',
                    array(
                        'module' => 'default',
                        'controller' => 'index',
                        'action' => 'index',
                    )
    );
    $router->addRoute('default', $defaultRoute);

    $partnerRoute = new Zend_Controller_Router_Route(
        ':partner',
        array('partner' => 'none'),
        array('partner' => '^(partner1|partner2|partner3)

Change as you see fit. In your controllers you will only get a value for the partner parameter if it was actually specified AND valid (you will get a routing error if the partner doesn't exist)...

) ); $router->addRoute('partner', $partnerRoute->chain($defaultRoute)); }

Change as you see fit. In your controllers you will only get a value for the partner parameter if it was actually specified AND valid (you will get a routing error if the partner doesn't exist)...

﹉夏雨初晴づ 2024-10-31 02:45:46

我在我的路线中使用类似的过程来 detech lang(但使用 ini 文件)。

您可以使用合作伙伴参数的默认值以使路由在没有合作伙伴的情况下正常工作,并向您的正则表达式添加 ?

但实际上,我不知道如何避免双重 //...

希望有帮助。

编辑:供您参考,这是我的路线的简化版本与语言:

routes.lang.type = "Zend_Controller_Router_Route"
routes.lang.route = "lang/:language/*"
routes.lang.reqs.language = "^(en|fr|nl|de)?$"
routes.lang.defaults.language = none
routes.lang.defaults.module = default
routes.lang.defaults.controller = index
routes.lang.defaults.action = language

I use a similar process to detech lang, in my route (but with a ini file).

You can use a default value for you partners parameter to make the route working without partner, and add a ? to your regex.

But actually, I don't know how to avoid the double //...

Hope that helps.

EDIT: For your information, here is a simplified version of my route with language:

routes.lang.type = "Zend_Controller_Router_Route"
routes.lang.route = "lang/:language/*"
routes.lang.reqs.language = "^(en|fr|nl|de)?$"
routes.lang.defaults.language = none
routes.lang.defaults.module = default
routes.lang.defaults.controller = index
routes.lang.defaults.action = language
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文