Zend framework ZF2是否将多个字符串路由到同一控制器?

Zend framework ZF2是否将多个字符串路由到同一控制器?,zend-framework,routing,routes,zend-framework2,url-routing,Zend Framework,Routing,Routes,Zend Framework2,Url Routing,我想配置我的Zf2应用程序,使多个字符串路由到同一个控制器。例如,www.mysite.com/this和www.mysite.com/that都路由到同一个控制器,并且使用$this->params可以捕获这个和那个。我怎样才能完成这样的事情?我需要两份单独的路线申报单吗 'directory' => [ 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => [ 'r

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

'directory' => [
     'type'      => 'Zend\Mvc\Router\Http\Literal',
     'options'   => [
          'route'     => '/string1 || /string2 || /string3',
          'defaults'  => [
               'controller' => 'Application\Controller\MyController',
               'action'     => 'index'
           ],
      ],
]
从创建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',
           ],
      ],
],
您可以使用路由类型,而不是文本类型,并执行以下操作

'directory' => [
    'type'      => 'Zend\Mvc\Router\Http\Regex',
    'options'   => [
        'route'     => '/string(?<id>[0-9]+)',
        'defaults'  => [
            'controller' => 'Application\Controller\MyController',
            'action'     => 'index'
        ],
    ],
]
“目录”=>[
'type'=>'Zend\Mvc\Router\Http\Regex',
“选项”=>[
'路由'=>'/string(?[0-9]+)',
“默认设置”=>[
'controller'=>'Application\controller\MyController',
'操作'=>'索引'
],
],
]

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路线是后进先出的。最好先插入它,然后处理任何需要“捕获”的情况

提到后进先出,是因为如果你在路由器阵列中定义“之后”的路由,这些路由将位于全面捕获之前,如果我正确阅读了你的问题,这似乎是有益的

干杯!
亚历克斯

像你提到的那样,走两条不同的路线