Ray.SymfonySessionModule
, (*1)
A Symfony Session Module for Ray.Di, (*2)
Installation
Composer install
$ composer require ray/symfony-session-module
Module install
PdoSessionModule (e.g. for MySQL)
- 
Create sessions table in your database., (*3)
$ ./vendor/ray/symfony-session-module/bin/initPdoSession 'mysql:host=localhost;dbname=mydb' 'myname' 'mypass'
 
- 
Install module., (*4)
use Ray\Di\AbstractModule;
use Ray\SymfonySessionModule\PdoSessionModule;
class AppModule extends AbstractModule
{
    protected function configure()
    {
        $pdo = new \PDO('mysql:host=localhost;dbname=mydb', 'myname', 'mypass');
        $options = [
            'cookie_secure' => 1,
            'cookie_httponly' => 1,
            'cookie_lifetime' => 60 * 60 * 24
        ];
        $this->install(new PdoSessionModule($pdo, $options));
    }
}
 
DI trait
- 
SessionInject for 
Symfony\Component\HttpFoundation\Session\SessionInterface interface 
Session lifetime management
For each request, your application can check whether session cookie is expired or not. If session cookie is expired, SessionExpiredException is thrown., (*5)
Configuration
- 
Install SessionalModule., (*6)
use Ray\Di\AbstractModule;
use Ray\SymfonySessionModule\PdoSessionModule;
use Ray\SymfonySessionModule\SessionalModule;
class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new PdoSessionModule($pdo, $options));
        $this->install(new SessionalModule); // <--
    }
}
 
- 
Mark the class or method with @Sessional annotation., (*7)
When any method in the class marked with @Sessional is executed, session is automatically started and session cookie is checked., (*8)
use Ray\SymfonySessionModule\Annotation\Sessional;
/**
 * @Sessional
 */
class SomeController
{
    public function fooAction()
    {
        // session is automatically started and session cookie is checked.
    }
}
When the method marked with @Sessional is executed, session is automatically started and session cookie is checked., (*9)
use Ray\SymfonySessionModule\Annotation\Sessional;
class SomeController
{
    /**
     * @Sessional
     */
    public function fooAction()
    {
        // session is automatically started and session cookie is checked.
    }
    public function barAction()
    {
        // session is NOT started.
    }
}
 
Unit Testing
MockSessionModule provides in-memory session mechanism for unit testing. Any session data are not persisted to the storage., (*10)
use Ray\Di\AbstractModule;
use Ray\SymfonySessionModule\MockSessionModule;
use Ray\SymfonySessionModule\SessionalModule;
class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new MockSessionModule); // <--
        $this->install(new SessionalModule);
    }
}
Demo
$ php docs/demo/run.php
// Session is started!
Requirements
More Documents