xorock / expressive-zend-session
Zend Session middleware for Zend Expressive
Installs: 4 852
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: ^5.5 || ^7.0
- container-interop/container-interop: ^1.1
- psr/http-message: ^1.0
- zendframework/zend-session: ^2.7
Requires (Dev)
- phpunit/phpunit: ^4.7
- squizlabs/php_codesniffer: ^2.3
Suggests
- mtymek/expressive-config-manager: Allows collecting and merging configuration from different sources
This package is not auto-updated.
Last update: 2025-01-04 21:30:38 UTC
README
Provides Zend Session integration for Expressive.
Install this library using composer:
$ composer require xorock/expressive-zend-session
Usage
I suggest installing Expressive Configuration Manager
Zend Session has built-in ConfigProvider
class, responsible for automatic registration of the components.
With Expressive Configuration Manager You can register all factories with just single line:
$configManager = new ConfigManager([ \Zend\Session\ConfigProvider::class, new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'), ], 'data/config-cache.php');
Now, Zend Session will search Your merged config file for predefined keys. Please refer to Zend Session documentation.
As an example, We can create following file:
session.global.php
use Zend\Session\Storage\SessionArrayStorage; return [ 'session_config' => [ 'name' => 'SID', 'cookie_httponly' => true, 'cookie_path' => '/', 'cookie_secure' => false, 'use_cookies' => true, 'cookie_lifetime' => 3600, 'save_path' => '/temp/data/session', ], 'session_storage' => [ 'type' => SessionArrayStorage::class ], ];
Note: There is a bug in SessionManager which can lead to fatal error when using SessionStorage class.
Now You can register middleware:
middleware-pipeline.global.php
use Mylab\Session\ZendSessionMiddleware; use Mylab\Session\ZendSessionMiddlewareFactory; // ... 'dependencies' => [ 'factories' => [ Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class, Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class, ZendSessionMiddleware::class => ZendSessionMiddlewareFactory::class, ], ], 'middleware_pipeline' => [ 'always' => [ 'middleware' => [ Helper\ServerUrlMiddleware::class, ZendSessionMiddleware::class, ], 'priority' => 10000, ] ]
Middleware injects SessionManager
to Container
so You can get it with:
use Zend\Session\Container; Container::getDefaultManager();
How can I use Session Save Handler?
When created, SessionManagerFactory
searches Container
for addition keys. One of them is
$saveHandler = $container->get(SaveHandlerInterface::class);
Simply, if you wish to attach a save handler to the manager, you will need to write Your own factory, and assign it to the service name "Zend\Session\SaveHandler\SaveHandlerInterface", (or alias that name to your own service).