Damaged Organic
Damaged Organic

Reputation: 8467

Symfony2 - passing an object from Event Listener to Controller

I have an Event Listener that finds an entity object in the database an checks whether current user is authorized for it. I need this object further in Controller, so my question is: what is the best way to pass an object from Event listener to Controller?

I have several options, but none of them is good enough.

  1. In Controller, using repository to fetch the same object from database second time - which is actually less coupled but produces additional DB query.
  2. Serializing object in Event Listener and passing it to Controller in request.
  3. Making a service that will set() object in Event Listener and get() in Controller - but it looks like architectural flaw to assign service for such purposes.

Maybe anyone has better solution?

Upvotes: 0

Views: 808

Answers (1)

Carlos Granados
Carlos Granados

Reputation: 11351

An Event Listener is a service like any other service. You can save your object to a protected member of your service, create a getter for it and in the controller get this service and retrieve the objet using the getter

Upvotes: 1

Related Questions