I'm trying to add header authentication to my zf2 app, binding a listener to the dispatch event.
My listener is properly executed on dispatch, and returns error and stop the response from propagating as it should.
The problem is that, when everything is ok, i'd like to set the current users parameter in the request so that i can get it from the controller.
Here is my code right now :
Module.php
public function onBootstrap(MvcEvent $event)
{
$app = $event->getApplication();
$sm = $app->getServiceManager();
$em = $app->getEventManager();
$listener = $sm->get('MyAuthListener');
$em->getSharedManager()->attach(
'eventIdentifierForMyAbstractController',
MvcEvent::EVENT_DISPATCH,
$listener
);
}`
public function __invoke(MvcEvent $event)
{
$result = $this->adapter->authenticate();
if (!$result->isValid()) {
//Invalidation process
}
/validation Process
$event->setParam('user', $result->getIdentity());
}
$result->getIdentity()
$event->setParam('user', $result->getIdentity())
$this->getEvent()->getParam('user')
{}
var_dump($result->getIdentity())
$this->getEvent()->getParam('hello', 'hello')
You must give your listener a higher priority to make it execute before the listener which dispatches the controller action. For example 100.
$em->getSharedManager()->attach(
'eventIdentifierForMyAbstractController',
MvcEvent::EVENT_DISPATCH,
$listener,
100
);