Skip to content

Repository files navigation

Ray.SymfonySessionModule

Scrutinizer Code QualityCode CoverageBuild StatusPackagistPackagist

A Symfony Session Module for Ray.Di

Installation

Composer install

$ composer require ray/symfony-session-module

Module install

PdoSessionModule (e.g. for MySQL)

  1. Create sessions table in your database.

    $ ./vendor/ray/symfony-session-module/bin/initPdoSession 'mysql:host=localhost;dbname=mydb''myname''mypass'
  2. 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));
    }
    }

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.

Configuration

  1. Install SessionalModule.

    useRay\Di\AbstractModule;
    useRay\SymfonySessionModule\PdoSessionModule;
    useRay\SymfonySessionModule\SessionalModule;
    class AppModule extends AbstractModule
    {
    protectedfunctionconfigure()
    {
    $this->install(newPdoSessionModule($pdo, $options));
    $this->install(newSessionalModule); // <--
    }
    }
  2. Mark the class or method with @Sessional annotation.

    When any method in the class marked with @Sessional is 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 @Sessional is 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.
    }
    }

Unit Testing

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);
}
}

Demo

$ php docs/demo/run.php
// Session is started!

Requirements

  • PHP 5.6+
  • hhvm

More Documents

About

A Symfony Session Module for Ray.Di

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages