Zend framework2 一条路由上有多个控制器
假设我有一个相当标准的路由定义,比如说,用户的东西:Zend framework2 一条路由上有多个控制器,zend-framework2,zend-route,Zend Framework2,Zend Route,假设我有一个相当标准的路由定义,比如说,用户的东西: 'router' => array( 'routes' => array( 'user' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/user[/:action]',
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/user[/:action]',
'constraints' => array('action' => '[a-zA-Z0-9_-]*'),
'defaults' => array(
'controller' => 'usercontroller',
'action' => 'index',
),
),
),
),
),
现在,假设我想为不同的“用户”操作组使用不同的控制器。例如,假设一个或两个动作(“特殊”和“超级”)应该转到“specialcontroller”。我如何配置它?我尝试过使用“child_routes”但没有效果,我尝试过在“routes”数组中使用多个“user”条目,但没有乐趣。您可以创建一个带有子路由类型段的路由类型文字:
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
),
),
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
),
),
),
或者,如果愿意,直接将控制器名称声明为管线类型段中的管线参数:
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
),
),
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
),
),
),
我更喜欢第一种方法来避免模块控制器之间的路由冲突
如果应用程序的特定部分(此处:用户)有多个控制器,并且应用程序的多个部分控制在同一个模块中,您还可以做什么=>在不同的命名空间中组织控制器,如:
namespace MyModule\Controller\Users;
可以使用子管线类型段创建管线类型文字:
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
),
),
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
),
),
),
或者,如果愿意,直接将控制器名称声明为管线类型段中的管线参数:
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
),
),
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'defaults' => array(
'__NAMESPACE__' => 'MyModule\Controller',
'controller' => 'User',
'action' => 'index',
),
),
),
),
),
我更喜欢第一种方法来避免模块控制器之间的路由冲突
如果应用程序的特定部分(此处:用户)有多个控制器,并且应用程序的多个部分控制在同一个模块中,您还可以做什么=>在不同的命名空间中组织控制器,如:
namespace MyModule\Controller\Users;
对于那些像我一样仍在学习ZF2并希望使用更简单版本的伟大答案的人,下面是代码:
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'usercontroller',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array('action' => '[a-zA-Z0-9_-]*'),
'defaults' => array(
'controller' => 'usercontroller',
'action' => 'index',
),
),
),
'special' => array(
'type' => 'Literal',
'options' => array(
'route' => '/details',
'defaults' => array(
'controller' => 'specialcontroller',
'action' => 'special',
),
),
),
),
),
要使“$this->url”在您的视图代码中工作,您现在需要对其进行稍微不同的设置。对于默认分组中的操作,它变为:
$this->url('user/default', array('action'=>'whatever'))
而对于特殊情况,严格来说,应:
$this->url('user/special')
但是,如果您所做的只是改变动作所指向的控制器,那么
$this->url('user/default', array('action'=>'special'))
还应努力产生正确的链接
还有一个非常有用的zf2备忘,网址为:,对于像我这样仍在学习zf2并希望获得更简单版本的伟大答案的人,代码如下:
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'usercontroller',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array('action' => '[a-zA-Z0-9_-]*'),
'defaults' => array(
'controller' => 'usercontroller',
'action' => 'index',
),
),
),
'special' => array(
'type' => 'Literal',
'options' => array(
'route' => '/details',
'defaults' => array(
'controller' => 'specialcontroller',
'action' => 'special',
),
),
),
),
),
要使“$this->url”在您的视图代码中工作,您现在需要对其进行稍微不同的设置。对于默认分组中的操作,它变为:
$this->url('user/default', array('action'=>'whatever'))
而对于特殊情况,严格来说,应:
$this->url('user/special')
但是,如果您所做的只是改变动作所指向的控制器,那么
$this->url('user/default', array('action'=>'special'))
还应努力产生正确的链接
还有一个非常有用的zf2备忘表,位于: