Zend framework Zend_控制器_路由器_路由_正则表达式可选参数,带反向

Zend framework Zend_控制器_路由器_路由_正则表达式可选参数,带反向,zend-framework,zend-route,zend-view,Zend Framework,Zend Route,Zend View,我已经使用正则表达式成功创建了路线。我的路由中有几个可选参数,除非用户指定,否则我不希望在URL帮助器中显示这些参数。我怎样才能做到这一点 这就是我现在拥有的 $route = new Zend_Controller_Router_Route_Regex( '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))', array( 'controller' =

我已经使用正则表达式成功创建了路线。我的路由中有几个可选参数,除非用户指定,否则我不希望在URL帮助器中显示这些参数。我怎样才能做到这一点

这就是我现在拥有的

        $route = new Zend_Controller_Router_Route_Regex(
        '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
        ),
        array(
            1 => 'color',
            2 => 'page',
            3 => 'limit'

        ),
        '%s-Widgets/'
    );

    $router->addRoute('color_widgets', $route);
然后用以下代码调用URL帮助器

        echo $this->url(array('page' => $page), 'color_widgets', false); 
        echo $this->url(array('page' => $page), 'color_widgets', false); 
这将导致/Blue Widgets/,并且不会将页面发送到URL。我可以通过改变路由器的反转来解决这个问题

    $route = new Zend_Controller_Router_Route_Regex(
        '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
            'page'      => 1
        ),
        array(
            1 => 'color',
            2 => 'page',
            3 => 'limit'

        ),
        '%s-Widgets/page/%d'
    );
但是这并不能解决我的问题,比如说我有一个Url

/Blue Widgets/page/1/limit/10限制没有显示,我可以用下面的方法修复它

    $route = new Zend_Controller_Router_Route_Regex(
        '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
            'page'      => 1,
            'limit'     => 10
        ),
        array(
            1 => 'color',
            2 => 'page',
            3 => 'limit'

        ),
        '%s-Widgets/page/%d/limit/%d'
    );
这方面的问题是用户处于 /Blue Widgets/我想用下面的代码将它们带到Blue Widgets的下一页

        echo $this->url(array('page' => $page), 'color_widgets', false); 
        echo $this->url(array('page' => $page), 'color_widgets', false); 
他们实际上被带到了 /蓝色小部件/页面/2/limit/10

当我真的想带他们去 /蓝色小部件/page/2


如何使用Zend框架实现这一点

不可能使用具有可变数量值的正则表达式反向路由。 你可以:

  • 为每个可选参数编写不同的路由(不推荐)
  • 使用不同的路由结构
  • 例如,您可以将路线更改为:

    $route = new Zend_Controller_Router_Route(
        'widgets/:color/*',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
            'page'      => 1,
            'limit'     => 10
        ),
        array(
            'color' => '[a-zA-Z-_0-9-]+',
            'page' => '\d+',
            'limit' => '\d+',
        )
    );
    

    另一个选择是创建您自己的自定义路由类,它可以解析和构建正确的uri。

    您给出了错误的正则表达式匹配变量索引,这就是为什么会得到奇怪的结果。您的代码应该如下所示:

    $route = new Zend_Controller_Router_Route_Regex(
    '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
    array(
        'controller'    => 'widget',
        'action'        => 'list',
    ),
    array(
        1 => 'color',
        3 => 'page',
        5 => 'limit'
    ),
    '%s-Widgets/'
    );
    
    $router->addRoute('color_widgets', $route);
    

    谢谢,我一直在努力避免写自定义路线。我将尝试使用Router_Route。我知道这很旧-几乎和我目前使用的代码一样旧;)-但是对于任何发生在这件事上的人,我想我应该指出这不正确的原因是
    (?:
    开始了一个非捕获组;