ray/symfony-session-module

A Symfony Session Module for Ray.Di

1.1.0 2016-07-08 12:33 UTC

This package is not auto-updated.

Last update: 2024-11-07 03:16:14 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Packagist Packagist

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.

    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.

Configuration

  1. Install SessionalModule.

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

    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.

    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.

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

  • PHP 5.6+
  • hhvm

More Documents