ZF2,在同一个地方使用身份证或行动路线?

时间:2015-01-21 13:23:13

标签: routes zend-framework2

我想要这条路线:

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中编写它?

2 个答案:

答案 0 :(得分:0)

为操作创建子路由,并在路径main中仅为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',
                    ),
                ),
            ),
        ),
    ),
),

答案 1 :(得分:0)

其他解决方案是

'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'
                ),
            ),
        ),
    ),

用户/任何行动无法正常工作

用户//任何操作工作,使用双斜杠。