0

Is there a way to listen to a request event and load an entity which will be available within all controllers? Basically, I want to intercept the request and load a certain "Site" entity based on the host name. I want this Site entity to be available to all controllers without them having to call the service that does all of this.

1 Answer 1

1

I would make some sort of a HelperController, so you don't have to put the code in all the controllers.

Your controllers won't extend the ordinary Controller, but an abstract HelperController, which will extend the framework's Controller class.

Then inside your helper controller, make a method getSite(), which will call the entity manager service and load & return your entity. I think this is a very clean approach.

Sign up to request clarification or add additional context in comments.

7 Comments

That would work. What about an event listener which sets the Site entity as a session variable? That way, the Site entity would be available in all controllers with no modification?
It would probably work. I would personally prefer to use ApcCache object instead of Session object. Take a look for an example here: blog.alterphp.com/2011/08/…
I really don't think a HelperController or using a session is the best way to go here. Just have your listener create the entity then store it in the request object. Your controller can pull it from there or you can even have it as a parameter to your controller action method.
Cerad, how can I store the entity in the request object and/or use it as a controller parameter?
I once had a doctrine entity saved in session by a listener, unserialized. Just to let you know:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.