Set url parameter in redirect in zend framework v2

Sina Miandashti picture Sina Miandashti · May 18, 2012 · Viewed 38k times · Source

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

Answer

Rid Zeal picture Rid Zeal · Oct 6, 2013

This one is working example. The route script

$this->redirect()->toRoute('myaccount', array(
    'controller' => 'admin',
    'action' =>  'index',
    'param1' =>'updated',
    'param2'=>'1'
));

Then, setting the parameter in module.config.php

'myaccount' => array(
    'type' => 'Segment',
    'options' => array(
    'route'    => '/myaccount[/:action][/:param1][/:param2]',
    'defaults' => array(
            'controller' => 'Main\Controller\MyAccount',
            'action'     => 'index',
        ),
    ),
),

This will bring you to MyAccountController, indexAction with param1='updated' and param2='1'. But in your example case, the action should be update with parameter name 'update' and parameter value '1'