ZF2: How to pass parameters to forward plugin which I can then get in the method I forward them to?

Green picture Green · Dec 7, 2012 · Viewed 15.1k times · Source

I have an Action method in Foo Controller which requires parameters:

public function fooAction($one, $two) {
    $a = one;
    $b = $two;
}

And I need to forward to that method from the other method of some Boo Controller. And one of those parameters has to be by reference parameter. The only example that the manual has is this:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));

No any additional parameters. But they write:

$params is an optional array of parameters with which to see a RouteMatch object for purposes of this specific request.

So, I tried:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
                                                      'action' => 'boo',
                                                      'one' => &$one,
                                                      'two' => $two,
                                                 ));

But it doesn't work.

Is there any way to pass additional parameters to forward controller?

UPD:

These do not work too:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
                                                      'action' => 'boo',
                                                      'params' => array(
                                                          'one' => &$one,
                                                          'two' => $two,
                                                     )));


$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
                                                      'action' => 'boo',
                                                      'options' => array(
                                                          'one' => &$one,
                                                          'two' => $two,
                                                     )));

UPD 2:

I still can't get the functionality I want (to pass parameters with the forward plugin) but I found other solutions. Before calling the forward plugin I set the variables to the Request object and after the forward I get them from the Request in my boo Action of my Boo\Controller\BooController:

// in Foo::fooAction
$this->getRequest()->one = &$one;
$this->getRequest()->two = $two;
$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));

// in Boo::booAction
$a = $this->getRequest()->one;
$b = $this->getRequest()->two;

Stupid solution, it will not work with Ajax requests. Still interested how to pass parameters with the forward plugin. OR MAYBE how to get them in the booAction. Because there in no anything in the Request if I pass them with the forward.

UPD 3 and Final:

I finally found where they've decided to hide parameters I pass with the forward plugin. They put them in the RouteMatch object.

- Tryyyy to guess where we've hidden your params... Oh yeeah, they are in the RouteMatch, of course they are there, didn't you think smth else?

And NO ANY info in the forward plugin section of the manual!

To get params, I have to do this in my BooController::booAction:

$param = $this->getEvent()->getRouteMatch()->getParam('nameOfParam');

Answer

sumeragi picture sumeragi · Dec 7, 2013

Why not to use the params plugin?

This works for me:

public function indexAction() {

        $object = new SomeObject();

        return $this->forward()->dispatch('Application\Controller\Index', [
                'action'     => 'show',
                'myObject'   => $object,
        ]);
    }

public function showAction() {

        $object = $this->params('myObject');

        var_dump($object);

        return [];
}