Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add event to initialize view context globally #2262

Open
pichelour opened this issue Aug 24, 2020 · 0 comments
Open

add event to initialize view context globally #2262

pichelour opened this issue Aug 24, 2020 · 0 comments

Comments

@pichelour
Copy link

Hello,

In our API, we must always initialize the same view context, based on a service.

I must add this in every controller:

        $view = $this->view($data, $status);
        $context = new Context();
        $context->setGroups($this->groupsResolver->getGroups());
        $view->setContext($context);
	
        return $this->handleView($view);

I made a trait to not duplicate code, it works but I feel it can be better.

I thought of adding an event in ViewHandler->handle, a listener can then do the job of setting the view context.

    public function handle(View $view, Request $request = null)
    {
        if (null === $request) {
            $request = $this->requestStack->getCurrentRequest();
        }

+       $this->eventDispatcher->dispatch(new HandleViewEvent($view, $request));

What do you think of it?

Thanks!

@pichelour pichelour changed the title add event to initialize viewHandler->exclusionStrategyGroups add event to initialize view context globally Aug 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant