A Symfony Session Module for Ray.Di
$ composer require ray/symfony-session-moduleCreate
sessionstable in your database.$ ./vendor/ray/symfony-session-module/bin/initPdoSession 'mysql:host=localhost;dbname=mydb''myname''mypass'
Install module.
useRay\Di\AbstractModule; useRay\SymfonySessionModule\PdoSessionModule; class AppModule extends AbstractModule { protectedfunctionconfigure() { $pdo = new \PDO('mysql:host=localhost;dbname=mydb', 'myname', 'mypass'); $options = [ 'cookie_secure' => 1, 'cookie_httponly' => 1, 'cookie_lifetime' => 60 * 60 * 24 ]; $this->install(newPdoSessionModule($pdo, $options)); } }
- SessionInject for
Symfony\Component\HttpFoundation\Session\SessionInterfaceinterface
For each request, your application can check whether session cookie is expired or not. If session cookie is expired, SessionExpiredException is thrown.
Install
SessionalModule.useRay\Di\AbstractModule; useRay\SymfonySessionModule\PdoSessionModule; useRay\SymfonySessionModule\SessionalModule; class AppModule extends AbstractModule { protectedfunctionconfigure() { $this->install(newPdoSessionModule($pdo, $options)); $this->install(newSessionalModule); // <-- } }
Mark the class or method with
@Sessionalannotation.When any method in the class marked with
@Sessionalis executed, session is automatically started and session cookie is checked.useRay\SymfonySessionModule\Annotation\Sessional; /** * @Sessional */class SomeController { publicfunctionfooAction() { // session is automatically started and session cookie is checked. } }
When the method marked with
@Sessionalis executed, session is automatically started and session cookie is checked.useRay\SymfonySessionModule\Annotation\Sessional; class SomeController { /** * @Sessional */publicfunctionfooAction() { // session is automatically started and session cookie is checked. } publicfunctionbarAction() { // session is NOT started. } }
MockSessionModule provides in-memory session mechanism for unit testing. Any session data are not persisted to the storage.
useRay\Di\AbstractModule;
useRay\SymfonySessionModule\MockSessionModule;
useRay\SymfonySessionModule\SessionalModule;
class AppModule extends AbstractModule
{
protectedfunctionconfigure()
{
$this->install(newMockSessionModule); // <--$this->install(newSessionalModule);
}
}$ php docs/demo/run.php
// Session is started!- PHP 5.6+
- hhvm
- the official documentation about Session Management of Symfony

