2
votes

In ZF2, I have a controller plugin that makes a remote call to an authentication service. I want to pass the Set-Cookie headers from this call to my Response in order to pass through the cookies set in the first request to the client's browser.

How can I do this in an controller plugin, where I don't have an available instance of the Resquest/Response class?

1

1 Answers

2
votes

There are a few ways.

If your controller plugin extends Zend\Mvc\Controller\Plugin\AbstractPlugin you can call (from within your plugin).

$this->getController()->getResponse();

Another way could be to inject the response object as a constructor argument.

// Module.php
public function getControllerPluginConfig()
{
    return array(
        'factories' => array(
            'MyModule\Controller\FooPlugin' => function($pm) {
                $serviceManager = $pm->getServiceLocator();
                $response = $serviceManager->get('Response');

                return new FooPlugin($response);
            }
        )
    );
}