2
votes

My question about a little, but significant detail that i discover while read this article: http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html

Manual says, that declared TokenListener will be executed on each request. So, if i have a big application with a lot of controllers (or bundles with own controllers), and a lot of any other kind of event listeners, this beaviour add an overhead: each of the event listeners will be executed in each request for each bundle! For example, i have default AppBundle and ApiBundle, and in given example, TokenListener executes in case of request to boths bundles, because defined in app specific config: app/config/services.yml

How to avoid this beaviour? How to define listeners only per bundle, or, better, per controller? Maybe,it can be defined in bundle specific services.yml, but this is also too wide scope for listener, that used in a few controllers.

1
You might be mixing up the concepts of controllers and listeners. Token listeners are Request listeners and are processed before the controller is known so assigning listeners per controller makes little sense. Look at the HttpKernel::handle code to see the workflow. It's not as bad as you seem to think.Cerad

1 Answers

0
votes

Please note that there probably is only one requests each time you open a page (apart from subrequests). The listener is thus only called once.

If you also look at the code of the listener, you can see that it already does only perform the "complex logic" when the controller is instance of TokenAuthenticatedController. This means it already quite does what you said you wanted: only execute for some controllers.

The only overhead one has is the call of the listener. This doesn't add much overhead.

Doing something to prevent the listener from being called when controller isn't an instance of TokenAuthenticatedController means you're just shifting the instanceof check from listener to a method before listener, ending up with exactly the same overhead.