1

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.

1 Answer 1

1

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like this is the only solution. I was hoping for something like TempData in ASP MVC. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.