phpzend-framework2zend-framework-mvclaminas-api-toolszend-framework-routing

ZF2: How to change Request object globally for all Controllers when using forward plugin?


I'm using forward plugin in testing and performance purposes. At first IndexController data passes through normal POST request. There I get this requst and POST data and I need add one more parameter to it.

$this->getRequest()->getPost()->subsystem = 'avia';

Than I use forward plugin

$result = $this->forward()->dispatch(
        "Port\\Controller",
        [
            'controller' => 'Port\\Controller',
            'action'     => 'port',
        ]
    );

And whan I'm in this PortController I would get my request POST data again and it SHOULD contain my changes from IndexController

$post              = $this->getRequest()->getPost();
isset($post['subsystem']) //true

But it does't. It get's request object without changes.

isset($post['subsystem']) //FALSE

How to change Request globally for all controllers in current request process?

What i'm already trying?

        //#1
        $params = $this->getServiceLocator()->get('ControllerPluginManager')->get('params');
        $params->getController()->getRequest()
               ->getPost()->subsystem
                = 'avia';

        //#2
        $this->getRequest()->getPost()->subsystem = 'avia';

        //#3
        $post              = $this->getRequest()->getPost();
        $post['subsystem'] = 'avia';
        //NEED UPDATE GLOBALLY !
        $this->getRequest()->setPost($post);

        //#4
        $event                                     = $this->getEvent();
        $event->getRequest()->getPost()->subsystem = 'avia';
        Debug::vars($event->getRequest()->getPost());

        //#5
        $_POST = $post->toArray();

And all this variances not working.

I'm already read this answer ZF2: How to pass parameters to forward plugin which I can then get in the method I forward them to?

But I don't want pass data through params, I need change Request.

UPD

But now i'm tested and maybe it was because on receiver side I tried to get request this way

 $request = $this->bodyParams();

But I should use it like this

if (!$request['subsystem']) {
      $request = $this->getRequest()->getPost()->toArray();
 }

It was because I used Apigility RPC service and placed post data in JSON format in Request Content field, not in POST. And in another place I tried get it

$params         = $this->serviceLocator->get('ControllerPluginManager')->get('params');
        $requestContent = $params->getController()->getRequest()->getContent();
        $request        = Json::decode($requestContent, Json::TYPE_ARRAY);

But after I started to use POST and that's why it started to be confused.


Solution

  • I am not sure if this is really something you should do but I think you should be able to achieve it like this:

    $parameters = $this->getRequest()->getPost();
    $parameters->set('subsystem', 'avia');
    

    $parameters is instance of Zend\Stdlib\Parameters.