I need to create a Service in PHP to manage the sessions of an application, does anyone have a practical example for this?
PS: I'm a beginner! :)
I need to create a Service in PHP to manage the sessions of an application, does anyone have a practical example for this?
PS: I'm a beginner! :)
You do not need to create a service to manage sessions in Symfony applications, since it already has a built-in service for that purpose called session
.
If you want to use session
in your controllers, just call it by service id:
$this->get('session');
... or use the method getSession()
of Symfony\Bundle\FrameworkBundle\Controller\Controller
:
$this->getSession();
If you want to create a service and inject the session into it (through the constructor), just pass this same session
on the service definition:
service:
class: AppBundle\Service\MyService
arguments: [ @session ]
Got it? :)