Pradeep shyam
Pradeep shyam

Reputation: 1292

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".

  '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" 

Upvotes: 1

Views: 3659

Answers (2)

been_there_done_that
been_there_done_that

Reputation: 33

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/settings/general[/][:action][/][:id]

or

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

should allow $this->redirect()->toRoute('admin/settings'); to work.

Upvotes: 2

cptnk
cptnk

Reputation: 2420

as the error message implies you need to add the "id" parameter. 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.

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.

Upvotes: 3

Related Questions