简体   繁体   English

在ZF2中重定向子路线会引发错误

[英]Redirecting for child routes in ZF2 throws an error

I've have an problem to redirect on child routes in zend framework 2. I can access the controller and action but while redirecting it throws me an error missing parameter "id". 我在zend Framework 2中的子路由上重定向时遇到问题。我可以访问控制器和操作,但是在重定向它时会抛出一个错误,提示我缺少参数“ id”。

  'admin' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/admin[/][:action][/:id]',
                        'constraints' => array(
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id'     => '[0-9]+',
                        ),
                        'defaults' => array(
                            'controller' => 'Admin\Controller\Admin',
                            'action'     => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                     'child_routes' => array(
                        'settings' => array(
                            'type'    => 'Segment',
                            'may_terminate' => true,
                            'options' => array(
                                'route'    => '/general[/][:action][/][:id]',
                                'constraints' => array(
                                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'id'     => '[0-9]+',
                                ),
                                'defaults' => array(
                                    'controller' => 'Admin\Controller\Settings\General',
                                    'action'     => 'index',
                                ),
                            ),
                        ),
                    ),
                ),

I have given redirect to route like below, 我已将重定向重定向至如下所示的路线,

return $this->redirect()->toRoute('admin/settings');

But it throws an error, 但这会引发错误,

Missing parameter "id" 

as the error message implies you need to add the "id" parameter. 由于错误消息暗示您需要添加“ id”参数。 You can redirect with a parameter like so. 您可以使用类似的参数进行重定向。

return $this->redirect()->toRoute('admin', array('action'=>'settings', 'id' => $id));

You did not show us your Controller action's but I assume 'settings' is a action within your admin module. 您没有向我们显示您的Controller动作,但我假设“设置”是您的管理模块中的动作。

At this point I cannot really see what kind of id the admin/settings function need's might aswell just try to add a 0 or 1 to try the route at first for testing purposes. 在这一点上,我真的看不到admin / settings函数需要什么样的ID,或者只是尝试添加0或1来首先尝试用于测试目的的路由。

The route matching method doesn't seem to 'consume' the parent node of the route. 路由匹配方法似乎并不“消耗”路由的父节点

Changing the child route to include the /admin part of the grammar to 更改子路径以将语法/ admin部分包括在内

/admin/settings/general[/][:action][/][:id] / admin / settings / general [/] [:action] [/] [:id]

or 要么

/admin/settings[/][:action][/][:id] / admin / settings [/] [:action] [/] [:id]

should allow $this->redirect()->toRoute('admin/settings'); 应该允许$this->redirect()->toRoute('admin/settings'); to work. 上班。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM