1

In Zend Framework 2 I want to pass an array of parameters from one action to another within the same controller which I did in ZF1 in the following manner:

$this->_helper->redirector->gotoSimple('foo', null, null, $params);

and in fooAction:

$params = $this->_request->getParams();

In ZF2, trying the various answers I have seen here on SO, I came up with the following:

$this->redirect()->toRoute('home/default', array(
            'controller' => 'client',
            'action' => 'foo',
            'param' => 'bar'),
        array('param' => 'bar'));

(trying both the $params and $options arguments of toRoute())

and in fooAction:

$param = $this->getEvent()->getRouteMatch()->getParams();

or

$param = $this->params()->fromRoute());

None works for me. Is there a simple way to achieve what I want (passing parameters with a redirect) or should I go the route of using a container, session or even global variables?

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
tihe
  • 2,452
  • 3
  • 25
  • 27
  • That should work perfectly well, are you sure your route is setup correctly? Are you making sure your controller implements InjectApplicationEvent (or extends the ZF2 Abstract controller provided) – Andrew Feb 14 '13 at 09:34
  • No, I haven't set up the parameters in my route. If I do that, it works indeed. But that means that I have to set up a route for each individual action with params. The beauty in ZF1 was that you could use a one for all (segment) route and set up individual params in your controller. – tihe Feb 15 '13 at 11:34
  • But you are using toRoute(), obviously this requires a route to work with ;) – Andrew Feb 15 '13 at 11:50
  • If you want to pass parameters as query string, here's your answer http://stackoverflow.com/a/15422349/1154069 . To access in controller, use $this->params()->fromQuery('paramName'); – webcoder Jul 17 '13 at 15:05
  • @Andrew: yes, that was were the real problem was, see my answer below. – tihe Jul 18 '13 at 16:26

2 Answers2

0

You could use the forward plugin:

http://framework.zend.com/manual/2.0/en/modules/zend.mvc.plugins.html#the-forward-plugin

public function someAction()
{
    $returnValue = $this->forward()->dispatch('application/controller/index', array(
        'action' => 'other'
    ));

    return $returnValue;
}

public function otherAction()
{
    return 99;
}

You will be able to pass parameters too

Andrew
  • 12,617
  • 1
  • 34
  • 48
  • But as far as I can see, in order to make that work, I need to set up the parameters in a route. And that was not necessary in ZF1. – tihe Feb 18 '13 at 08:59
0

In the end, what I did was, instead of using route parameters, using query parameters, since the parameters I used where not route related. That solved the problem.

tihe
  • 2,452
  • 3
  • 25
  • 27