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

你。 提交于 2019-12-03 13:22:06

You can create a container class and use it in both controllers

in module.conf

public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'my_handy_container'        => 'path\container_class_name',
        )
    );
}

Create a getter in both controllers:

public function getMyHandyContainer()
{
    if (!$this->myHandyContainer) {
        $this->myHandyContainer = $this->getServiceLocator()->get('my_handy_container');
    }
    return $this->myHandyContainer;
}

And call it using:

$myContainer = $this->getMyHandyContainer()->myHandyContainer;
$myContainer->foo = 5; // set something

ZF2 way to pass vars using forward

In the passing method do:

return $this->forward()->dispatch('controller_name', [
    'action' => 'whatever',
    'varname' => $value,
    'varname2' => $value2
]);

In the invoked controller method, do:

$param2 = $this->params()->fromRoute('varname2',false);

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 [];
}

Thought I would add another option that works for me.

You can simply pass the params straight through the forward function and use the routeMatch function to access them at the other end.

return $this->forward()
            ->dispatch('Module\Controller\Foo', array(
                                            'action' => 'bas', 
                                             'id' => 6)
                                              );

Passes to Foo Controller, basAction in this method you can then use the following code to access the id param

$myParam = (int) $this->getEvent()->getRouteMatch()->getParam('id');

Not sure if this meets your requirements - but works for me.

Thanks for the question, helped me a lot. Found an easy way for getting all params passed to forward()->dispatch(...). In the controller's action method:

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

returns array $data as passed as $data into forward()->dispatch($controllerName, $data).

Here in the official ZF2 documentation is written exactly how it works:

$params is an optional array of parameters with which to seed a RouteMatch object for purposes of this specific request. Meaning the parameters will be matched by their key to the routing identifiers in the config (otherwise non-matching keys are ignored).

So pass like this:

$params = array(
    'foo' => 'foo',
    'bar' => 'bar'
);
$this->forward()->dispatch('My\Controller', $params)

And then you can get your route match params in your My\Controller like normally:

$foo = $this->params()->fromRoute('foo');
$bar = $this->params()->fromRoute('bar');

For people struggling with accessing parameters within their controller here a nice overview from this CheatSheet.

$this->params()->fromPost('foo');  //POST
$this->params()->fromQuery('foo'); //GET
$this->params()->fromRoute('foo'); //RouteMatch
$this->params()->fromHeader('foo');//Header
$this->params()->fromFiles('foo'); //Uploaded file
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!