Unex Unex - 1 year ago 80
PHP Question

zf2 : Params from SharedEventManager to controller

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 :


public function onBootstrap(MvcEvent $event)
$app = $event->getApplication();
$sm = $app->getServiceManager();
$em = $app->getEventManager();

$listener = $sm->get('MyAuthListener');

This listener is the one supposed to do the work.

public function __invoke(MvcEvent $event)

$result = $this->adapter->authenticate();

if (!$result->isValid()) {
//Invalidation process
/validation Process
$event->setParam('user', $result->getIdentity());

On my side almost everything works.
is giving me the proper identity.
$event->setParam('user', $result->getIdentity())
sets properly the identity to the event in the listener.

But, in my controller when i'm doing :

The parameter is empty (i.e =
), but if i
, the object is fully created.

I can pass a param
$this->getEvent()->getParam('hello', 'hello')
, and this works fine.

I can't figure out why. The documentation says it expects a mixed type so it should be ok.

If you need more information feel free to ask.

Answer Source

You must give your listener a higher priority to make it execute before the listener which dispatches the controller action. For example 100.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download