Zend 2:为路由的动作添加前缀

时间:2015-09-28 15:26:15

标签: zend-framework2 zend-route

我有以下网址的应用

www.myapp.com/admin/profile           [index action]
www.myapp.com/admin/profile/add       [add action]
www.myapp.com/admin/profile/edit      [edit action]
www.myapp.com/admin/profile/del       [delete action]

这些网址适用于拥有管理员权限的用户 没有管理员权限的用户拥有以下网址

www.myapp.com/user/profile           
www.myapp.com/user/profile/add       
www.myapp.com/user/profile/edit      
www.myapp.com/user/profile/del

Zend 2中的路线

        // admin routes
        'admin' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    '__NAMESPACE__' => __NAMESPACE__.'\Controller',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(

                // Profile
                'profile' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route' => '/profile',
                        'defaults' => array(
                            '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                            'controller' => 'Profile',
                            'action' => 'index'
                        )
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'default' => array(
                            'type' => 'Segment',
                            'options' => array(
                                'route' => '[/:action[/:id]]',
                                'constraints' => array(
                                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'id' => '\d+'
                                ),
                                'defaults' => array(
                                    '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                                    'controller' => 'Profile'
                                )
                            )
                        ),
                    )
                ),
            ),
        ),


        // user routes
        'admin' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/user',
                'defaults' => array(
                    '__NAMESPACE__' => __NAMESPACE__.'\Controller',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(

                // Profile
                'profile' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route' => '/profile',
                        'defaults' => array(
                            '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                            'controller' => 'Profile',
                            'action' => 'clientIndex'
                        )
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'default' => array(
                            'type' => 'Segment',
                            'options' => array(
                                'route' => '[/:action[/:id]]',
                                'constraints' => array(
                                    'action' => 'client[a-zA-Z][a-zA-Z0-9_-]*',    <----- look here
                                    'id' => '\d+'
                                ),
                                'defaults' => array(
                                    '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                                    'controller' => 'Profile'
                                )
                            )
                        ),
                    )
                ),
            ),
        ),

我想要的是,如果用户访问网址:www.myapp.com/admin/profile/add
应用程序运行控制器配置文件和操作 addAction

如果用户访问网址:www.myapp.com/user/profile/add
应用程序运行控制器配置文件和操作 userAddAction

所以我想添加前缀&#39; user&#39; 用户路由时的每个操作。

我试过了:

                      'options' => array(
                            ...
                            'defaults' => array(
                                '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                                'controller' => 'Profile',
                                'action' => 'user[:action]'     <--- look here
                            )
                        )

但当然它不起作用。

我知道我可以定义静态路由,但我想用Segment路由类型

来做

1 个答案:

答案 0 :(得分:0)

无需更改路线。路由定义由URL 匹配,因此应用程序能够执行正确的控制器操作。

因此,通过定义'action' => 'client[a-zA-Z][a-zA-Z0-9_-]*',您说路由[:action]部分需要与此模式匹配,这只会在您浏览{{1}时发生等等。

要解决您的问题,您可以拆分子路线,以便每个操作都有一条路线。

此示例显示了用户路线,但您可以将其复制并将其添加为www.myapp.com/admin/profile/clientadd路线的子项(更改操作值点/admin/profile而不是addAction等)

clientAddAction

您需要做的唯一更改是在视图中呈现网址时。

// user routes
'user' => [
    // same as before
    'child_routes' => [

        'profile' => [
            // same as before
            'child_routes' => [

                'add' => [
                    'type' => 'literal',
                    'options' => [
                        'route' => '/add'
                        'defaults' => [
                            // no need for controller value as is
                            // inherited from the 'profile' parent route
                            'action' => 'clientAddAction'
                        ],
                    ],
                ],

                'edit' => [
                    'type' => 'segment',
                    'options' => [
                        'route' => '/edit/:id',
                        'defaults' => [
                            'action' => 'clientEditAction',
                        ]
                    ],
                ],

                'del' => [
                    'type' => 'segment',
                    'options' => [
                        'route' => '/del/:id',
                        'defaults' => [
                            'action' => 'clientDeleteAction',
                        ]
                    ],
                ],
            ],
        ],
    ],
];

会变成。

$this->url('/users/profile/default, ['action'=>'add']);
$this->url('/users/profile/default, ['action'=>'edit', 'id' => 123]);