I want to run some session handling code every time a URL or route is called, but I obviously want to be able to access this session variable in any controller I wish. Am I wrong in assuming that this should go in the front controller of my bundle?
I tried putting some variables in the ...Bundle.php file at the root of my bundle’s src, in DefaultController.php (I created a constructor) and more. Nothing works.
How can I run some code and set up some variables for every route?
Notes:
- I’m new to MVC and Symfony2.
- "every route" == every controller in the bundle I’m talking about
I would recommend implementing this as a kernel event listener, and injecting the session into it.
Elaborating:
If you look at http://symfony.com/doc/current/cookbook/service_container/event_listener.html you will see how to register an event listener.
Basically, you use this method to create a kernel event listener that runs every time the system fires up, and also inject the the session (@session in yml) to do what you need to with it.