vendredi 24 février 2017

How to set contextual value object in Symfony?

I extract some information from master request using a listener on kernel.request. I want to information to be available to all controller and maybe other listeners, services.

Since it is a value object UserContext I don't want to make it into a service.

What is the best way/pattern for setting this contextual value object so it is available everywhere?

My current idea is to set it to $request object:

public function onKernelRequest(GetResponseEvent $event)
{
    if (HttpKernel::MASTER_REQUEST !== $event->getRequestType()) {
        return;
    }

    $request = $event->getRequest();

    $userContext = $this->userContextFactory->createFromRequest($request);

    $request->attributes->set('_user_context', $userContext);
}

Aucun commentaire:

Enregistrer un commentaire