【问题标题】:ZF2 Route multiple strings to the same controller?ZF2 将多个字符串路由到同一个控制器?
【发布时间】:2015-09-03 04:06:34
【问题描述】:

我想配置我的 Zf2 应用程序,使多个字符串路由到同一个控制器。例如 www.mysite.com/this 和 www.mysite.com/ 都路由到同一个控制器,并且可以使用 $this->params 捕获这个和那个。我将如何完成这样的事情?我需要 2 个单独的路由声明吗?

'directory' => [
     'type'      => 'Zend\Mvc\Router\Http\Literal',
     'options'   => [
          'route'     => '/string1 || /string2 || /string3',
          'defaults'  => [
               'controller' => 'Application\Controller\MyController',
               'action'     => 'index'
           ],
      ],
]

【问题讨论】:

  • 像你提到的那样制作两条单独的路线

标签: zend-framework routing routes zend-framework2 url-routing


【解决方案1】:

IMO 最简单的解决方案是:

        'varcatcher' => [
            'type' => 'Segment',
            'options' => [
                'route' => '[/[:tail]]',
                'defaults' => [
                    'controller' => '\Application\Controller\Index',
                    'action' => 'catch',
                    'module' => 'Application',
                ],
                'constraints' => [
                    'tail' => '[a-zA-z0-9_-]*'
                ],
            ],
            'may_terminate' => true,
        ],

然后在你的行动中处理它:

public function catchAction(){
    die( $this->params()->fromRoute('tail') );
}

因为 ZF2 路线是 LIFO。通过首先插入它并处理您需要“捕获”的任何情况来处理它可能是最佳选择。

提到后进先出,是因为如果您在路由器数组中“之后”定义路由,那么这些路由将在包罗万象之前,如果我正确阅读了您的问题,这似乎是有益的。

干杯! 亚历克斯

【讨论】:

    【解决方案2】:

    您可以使用 Zend\Mvc\Router\Http\Regex 路由类型,而不是文字类型,并执行类似的操作

    'directory' => [
        'type'      => 'Zend\Mvc\Router\Http\Regex',
        'options'   => [
            'route'     => '/string(?<id>[0-9]+)',
            'defaults'  => [
                'controller' => 'Application\Controller\MyController',
                'action'     => 'index'
            ],
        ],
    ]
    

    【讨论】:

      【解决方案3】:

      根据Literal route的定义创建3条路由:

      'directory1' => [
           'type'      => 'Zend\Mvc\Router\Http\Literal',
           'options'   => [
                'route'     => '/string1',
                'defaults'  => [
                     'controller' => 'Application\Controller\MyController',
                     'action'     => 'index',
                 ],
            ],
      ],
      'directory2' => [
           'type'      => 'Zend\Mvc\Router\Http\Literal',
           'options'   => [
                'route'     => '/string2',
                'defaults'  => [
                     'controller' => 'Application\Controller\MyController',
                     'action'     => 'index',
                 ],
            ],
      ],
      'directory3' => [
           'type'      => 'Zend\Mvc\Router\Http\Literal',
           'options'   => [
                'route'     => '/string3',
                'defaults'  => [
                     'controller' => 'Application\Controller\MyController',
                     'action'     => 'index',
                 ],
            ],
      ],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 2020-02-27
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        相关资源
        最近更新 更多