if you want to access the Session from a Controller you can use this->get('session');.
But How does it work from within an Extension?
I tried configuring it within the services.xml and tweaking the __construct($session) method:
<service id="myService" class="%myServie.class%">
<argument type="service" id="session"/>
</service>
didn’t work. (null-object)
I tried reaching it from the Request:
$request = Request::createFromGlobals();
$session = $request->getSession();
didn’t work. (null object)
currently I am providing my methods with the session which i retrieve in a Controller, but I’m not satisfied with this solution (I think it’s a dirty solution).
How do you do this the right way?
Regards,
Senad
You can solve this problem in many ways. One way is to use Dependency Injection which is used throughout the framework.
If you are passing the session to your extensions constructor, you are “injecting” the session into the class.
example code
Symfony 2 provides a way to inject this session automatically. To do this you need to create a service:
services.yml example code
Then in your controller you can get the service: