ZF2,在同一个地方有 ID 或动作的路线?
ZF2, Route with ID or action in the same place?
我想要这条路线:
localhost/users/1 -> show profile
localhost/users/ -> show list users
localhost/users/anyaction -> execute any action in UsersController.php
localhost/users/anyaction/23 -> execute any action, with optional parameters(23), in UsersController.php
如何使用 Zend Framework 2 在 module.config.php 中编写此代码?
为动作创建子路由,并且在主路由中只有ID数字。
'router'=>array(
'routes'=>array(
'users'=>array(
'type'=>'Segment',
'options'=>array(
'route' => '/users[/:id]',
'constraints' => array (
'id' => '[0-9]+' ,
),
'defaults' => array(
'controller' => 'Users\Controller\Users',
'action' => 'index'
),
),
'may_terminate' => true,
'child_routes' => array(
'action' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/[:action[/:id]]]',
'defaults' => array(
'controller' => 'Users\Controller\Users',
),
),
),
),
),
),
其他解决方案是
'routes'=>array(
'users'=>array(
'type'=>'Segment',
'options'=>array(
'route' => '/users[/:id][/:action]',
'constraints' => array(
'id' => '[0-9]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Users\Controller\Users',
'action' => 'index'
),
),
),
),
但是users/anyaction不行
users//anyaction 工作,带双斜杠。
我想要这条路线:
localhost/users/1 -> show profile
localhost/users/ -> show list users
localhost/users/anyaction -> execute any action in UsersController.php
localhost/users/anyaction/23 -> execute any action, with optional parameters(23), in UsersController.php
如何使用 Zend Framework 2 在 module.config.php 中编写此代码?
为动作创建子路由,并且在主路由中只有ID数字。
'router'=>array(
'routes'=>array(
'users'=>array(
'type'=>'Segment',
'options'=>array(
'route' => '/users[/:id]',
'constraints' => array (
'id' => '[0-9]+' ,
),
'defaults' => array(
'controller' => 'Users\Controller\Users',
'action' => 'index'
),
),
'may_terminate' => true,
'child_routes' => array(
'action' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/[:action[/:id]]]',
'defaults' => array(
'controller' => 'Users\Controller\Users',
),
),
),
),
),
),
其他解决方案是
'routes'=>array(
'users'=>array(
'type'=>'Segment',
'options'=>array(
'route' => '/users[/:id][/:action]',
'constraints' => array(
'id' => '[0-9]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Users\Controller\Users',
'action' => 'index'
),
),
),
),
但是users/anyaction不行
users//anyaction 工作,带双斜杠。