stefna / session
Session management for psr7/psr15
Requires
- php: ^8.2
- psr/http-server-middleware: ^1.0.1
- psr/log: ^3.0
- stefna/ds-collection: ^1.0
Requires (Dev)
- bnf/phpstan-psr-container: ^1.0
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.8
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^10.4
- squizlabs/php_codesniffer: dev-master
- stefna/codestyle: ^1.12
- tomasvotruba/type-coverage: ^0.0.7
README
The package helps in working with sessions in a PSR-7/PSR-15 environment.
It also provides a flash message service which helps use one-time messages.
Requirements
PHP 8.2 or higher.
Installation
composer require stefna/session
Session
In order to maintain a session between requests you need to add SessionMiddleware
to middleware collection.
Then you will have access to the session storage in the request object and can be retrieved
<?php $session = $request->getAttribute(\Stefna\Session\SessionStorage::class);
Flash Messages
You need to add FlashMiddleware
to your middleware runner, and it needs to be
added after the SessionMiddleware
.
Then you will have access to the flash messages in the request object and can be retried
<?php $messageCollection = $request->getAttribute(\Stefna\Session\Flash\FlashMessages::class);
Concept
The main concept of package is the separation of using the session from persisting the session.
This is achieved with lazy loading everything. The session is not started until someone needs something from the session.
Usage of session
<?php use Psr\Http\Message\ServerRequestInterface; class Action { public function __invoke(ServerRequestInterface $request) { $session = $request->getAttribute(\Stefna\Session\SessionStorage::class); if ($session->getBool('loggedIn')) { $session->set('loggedIn', false); } elseif ($session->has('blocked')) { $session->remove('blocked'); } } }
Usage of flash messages
Add flash messages
<?php use Psr\Http\Message\ServerRequestInterface; use Stefna\Session\Flash\FlashMessage; use Stefna\Session\Flash\FlashMessages; use Stefna\Session\Flash\MessageType; class Action { public function __invoke(ServerRequestInterface $request) { $flashMessages = $request->getAttribute(FlashMessages::class); $flashMessages->add(new FlashMessage('Stuff happened', type: MessageType::Live)); } }
Get flash messages
Getting the flash messages means that they will be removed from storage.
<?php use Psr\Http\Message\ServerRequestInterface; use Stefna\Session\Flash\FlashMessages; use Stefna\Session\Flash\MessageType; class Action { public function __invoke(ServerRequestInterface $request) { $flashMessages = $request->getAttribute(FlashMessages::class); $messages = $flashMessages->getMessages(MessageType::Live); $this->renderGrowMessages($messages); } }
Contribute
We are always happy to receive bug/security reports and bug/security fixes
License
The MIT License (MIT). Please see License File for more information.