logifire / nano-php-state
Session and cookie handling
0.2.0
2019-03-12 18:18 UTC
Requires
- php: ^7.2
- psr/http-server-middleware: ^1.0
Requires (Dev)
- logifire/nano-container: ^2.0.0
- logifire/nano-middleware: ^0.1.0
- nyholm/psr7: 1.0.1
- nyholm/psr7-server: 0.3.0
- phpstan/phpstan: ^0.11.2
- phpunit/phpunit: ^8.0.2
- zendframework/zend-httphandlerrunner: 1.0.1
This package is auto-updated.
Last update: 2024-12-15 08:31:49 UTC
README
The purpose of this library is to bridge the PHP session implementation, and the PSR 7, 15 standards.
Add the PhpStateMiddleware
to your middleware stack, and use the SessionService
to start a new session instead of session_start()
.
You can optionally use the SessionCollection
instead of $_SESSION
.
...
/* @var ServerRequestInterface $server_request */
$session_service = new SessionService($server_request);
// Starts a writable session, this is the default you are used to when calling session_start()
$session_service->startWriteRead();
// Now you can use sessions
$_SESSION['content'] = 'Hello World';
// Alternative
$session_collection = new SessionCollection();
$session_collection->setString('content', 'Hello World');
...
Cookie abstraction
This library also comes with a cookie abstraction, if you need to set custom cookies.
...
// The PhpStateMiddleware has an implicit dependency on ResponseCookieService, must be the same instance past though the app
$response_cookie_service = new ResponseCookieService();
...
$cookie = new ResponseCookie('name', 'value');
$cookie->setExpires(strtotime('+ 14 days'));
$response_cookie_service->addCookie($cookie);
...
NOTE
There is no implementation of the cache_limit
option. You should, however, be able to use most of the session options.