Alex Rivera | Logout

Set url parameter in redirect in zend framework v2

Asked 2012-05-18T11:42:38.637
12

I have following redirect script in my controller (Zend Framework 2)

return $this->redirect()->toRoute('default', array(
                        'controller' => 'admin',
                        'action' =>  'index'
));

Currently redirecting to localhost/zf2/public/admin/index

How can I redirect with an extra parameter?

Like:

localhost/zf2/public/admin/index/update/1

or localhost/zf2/public/admin/index/page/2

I have tried this :

return $this->redirect()->toRoute('default', array(
                        'controller' => 'admin',
                        'action' =>  'index'
                            'param' => 'updated/1'
                        ));

But is redirected to localhost/ttacounting/public/admin/index/updated%2F1

Edit
Report

2 Answers

7

This works for me.

The route:

'user_view' => array(
    'type'    => 'Segment',
    'options' => array(
        'route' => '/user/view[/:user_id]',
        'defaults' => array(
            'controller' => 'user',
            'action'     => 'viewUser',
        ),
    ),
), // End of user_view route

And the Redirect from the controller:

return $this->redirect()->toRoute('user_view', array('user_id'=>$user_id));

Notice that the Array key in the redirect statement correspond to the route segment: [/:user_id] = 'user_id'=>$user_id

answered 2012-05-19T12:57:24.243
1

This statement redirects you to localhost/leads/edit/112345:

return $this->redirect()->toRoute('leads', array(
                'action' => 'edit',
                'id' => 112345
            ));

id is the parameter I expect in the editAction.

Hope it helps.

answered 2012-11-14T16:44:51.220

Your Answer