Zend framework2 Zend 2:为路由添加前缀';s动作

Zend framework2 Zend 2:为路由添加前缀';s动作,zend-framework2,zend-route,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/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
这些URL适用于具有管理员权限的用户。
没有管理员权限的用户具有以下URL

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'
                                )
                            )
                        ),
                    )
                ),
            ),
        ),
//管理路由
“admin”=>数组(
'type'=>'Literal',
“选项”=>数组(
“路由”=>“/admin”,
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
),
),
“may_terminate”=>true,
“子路由”=>数组(
//侧面图
“profile”=>数组(
'type'=>'Literal',
“选项”=>数组(
“路由”=>“/profile”,
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
“控制器”=>“配置文件”,
'操作'=>'索引'
)
),
“may_terminate”=>true,
“子路由”=>数组(
'默认'=>数组(
'类型'=>'段',
“选项”=>数组(
“路由”=>“[/:action[/:id]]”,
'约束'=>数组(
“行动”=>“[a-zA-Z][a-zA-Z0-9_u-]*”,
'id'=>'\d+'
),
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
“控制器”=>“配置文件”
)
)
),
)
),
),
),
//用户路由
“admin”=>数组(
'type'=>'Literal',
“选项”=>数组(
“路由”=>“/用户”,
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
),
),
“may_terminate”=>true,
“子路由”=>数组(
//侧面图
“profile”=>数组(
'type'=>'Literal',
“选项”=>数组(
“路由”=>“/profile”,
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
“控制器”=>“配置文件”,
'操作'=>'客户端索引'
)
),
“may_terminate”=>true,
“子路由”=>数组(
'默认'=>数组(
'类型'=>'段',
“选项”=>数组(
“路由”=>“[/:action[/:id]]”,
'约束'=>数组(
“操作”=>“客户端[a-zA-Z][a-zA-Z0-9_u-]*”,“\d+”
),
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
“控制器”=>“配置文件”
)
)
),
)
),
),
),
我想要的是,如果用户访问url:www.myapp.com/admin/profile/add
应用程序运行控制器配置文件和操作添加操作

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

因此,当使用user路由时,我想在每个操作中添加前缀“user”

我试过:

                      'options' => array(
                            ...
                            'defaults' => array(
                                '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                                'controller' => 'Profile',
                                'action' => 'user[:action]'     <--- look here
                            )
                        )
“选项”=>数组(
...
“默认值”=>数组(
“\uuuu名称空间\uuuu”=>\uuuu名称空间\uuuu.'\Controller',
“控制器”=>“配置文件”,
“action'=>”用户[:action]”无需更改路由。路由定义将由URL匹配,因此应用程序能够执行正确的控制器操作

因此,通过定义
'action'=>“client[a-zA-Z][a-zA-Z0-9_-]*”
您是说
[:action]
路由的一部分需要与此模式匹配,这只有在您浏览
www.myapp.com/admin/profile/clientadd
等时才会发生

// 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',
                        ]
                    ],
                ],
            ],
        ],
    ],
];
要解决问题,您可以拆分子路由,以便每个操作都有一个路由

此示例显示了用户路由,但是您可以复制此路由并将其添加为
/admin/profile
路由的子级(更改操作值点
addAction
,而不是
clientAddAction
等)

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

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

$this->url('/users/profile/add');
$this->url('/users/profile/edit, ['id' => 123]);
无需更改路由。路由定义由URL匹配,因此应用程序能够执行正确的控制器操作

因此,通过定义
'action'=>“client[a-zA-Z][a-zA-Z0-9_-]*”
您是说
[:action]
路由的一部分需要与此模式匹配,这只有在您浏览
www.myapp.com/admin/profile/clientadd
等时才会发生

// 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',
                        ]
                    ],
                ],
            ],
        ],
    ],
];
要解决问题,您可以拆分子路由,以便每个操作都有一个路由

此示例显示了用户路由,但是您可以复制此路由并将其添加为
/admin/profile
路由的子级(更改