ray / symfony-session-module
A Symfony Session Module for Ray.Di
Requires
- php: >=5.6
- ray/di: ~2.3
- symfony/http-foundation: ~3.1
Requires (Dev)
- fabpot/php-cs-fixer: ~1.11
- phpmd/phpmd: ~2.4
- phpunit/phpunit: ~5.4
- squizlabs/php_codesniffer: ~2.6
This package is not auto-updated.
Last update: 2024-12-19 04:04:35 UTC
README
A Symfony Session Module for Ray.Di
Installation
Composer install
$ composer require ray/symfony-session-module
Module install
PdoSessionModule (e.g. for MySQL)
-
Create
sessions
table in your database.$ ./vendor/ray/symfony-session-module/bin/initPdoSession 'mysql:host=localhost;dbname=mydb' 'myname' 'mypass'
-
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
-
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); // <-- } }
-
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
- the official documentation about Session Management of Symfony