John
John

Reputation: 586

Pass parameters with redirect without putting them in url in Zend Framework 2

Is there a way to pass parameters with redirect in ZF2 like

return $this->redirect()->toRoute('account', array('test' => 'ok'));

and if possible, how can I get the test parameter?

PS. test is not a parameter mapped to the route. account is my controller and it has a default action, so I don't need to specify the action in redirect.

Upvotes: 1

Views: 5219

Answers (1)

Isolde
Isolde

Reputation: 636

I think you can do the following: Say your module is Myaccount and your controller is AccountController and your action is index (your default action I presume). So in your module.config.php you'd have something like this:

return array(
'controllers' => array(
    'invokables' => array(
        'Account' => 'Myaccount\Controller\AccountController', 

Then in some action you can pass parameters with redirect in ZF2 style like this:

return $this->redirect()->toRoute('account', array(
                   'controller' => 'account',
                   'action' =>  'index',
                       'test' => $testVal  // in this case "okay"
                   )); 

Oh yes, your route in your module.config.php is also important:

'router' => array(
    'routes' => array(
        'account' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/account[/:action][/:test]',

Because that's how ZF2 knows where to go and what to append to it, in our case it would lead to "../account/index/test/okay" as okay was our value for "test".

Hope it helps :)

Upvotes: 1

Related Questions