bryanjhv/slim-session
最新稳定版本:4.1.2
Composer 安装命令:
composer require bryanjhv/slim-session
包简介
Session middleware and helper for Slim framework 4.
README 文档
README
Simple middleware for Slim Framework 4, that allows managing PHP
built-in sessions and includes a Helper class to help you with the $_SESSION
superglobal.
For the middleware version for Slim Framework 3, please check out the slim-3
branch in this repository.
For the middleware version for Slim Framework 2, please check out the slim-2
branch in this repository.
Installation
Add this line to require block in your composer.json:
"bryanjhv/slim-session": "~4.0"
Or, run in a shell instead:
composer require bryanjhv/slim-session:~4.0
Usage
$app = \Slim\Factory\AppFactory::create(); $app->add( new \Slim\Middleware\Session([ 'name' => 'dummy_session', 'autorefresh' => true, 'lifetime' => '1 hour', ]) );
Supported options
lifetime: How much should the session last? Default20 minutes. Any argument thatstrtotimecan parse is valid.path,domain,secure,httponly,samesite: Options for the session cookie. Please note thatsamesiteis'Lax'by default, set to''to disable.name: Name for the session cookie. Defaults toslim_session(instead of PHP'sPHPSESSID).autorefresh:trueif you want session to be refresh when user activity is made (interaction with server).handler: Custom session handler class or object. Must implementSessionHandlerInterfaceas required by PHP.ini_settings: Associative array of custom session configuration. Previous versions of this package had some hardcoded values which could bring serious performance leaks (see #30):[ 'session.gc_divisor' => 1, 'session.gc_probability' => 1, 'session.gc_maxlifetime' => 30 * 24 * 60 * 60, ];
Session helper
A Helper class is available, which you can register globally or instantiate:
$container = new \DI\Container(); // Register globally to app $container->set('session', function () { return new \SlimSession\Helper(); }); \Slim\Factory\AppFactory::setContainer($container);
That will provide $app->get('session'), so you can do:
$app->get('/', function ($req, $res) { // or $this->get('session') if registered $session = new \SlimSession\Helper(); // Check if variable exists $exists = $session->exists('my_key'); $exists = isset($session->my_key); $exists = isset($session['my_key']); // Get variable value $my_value = $session->get('my_key', 'default'); $my_value = $session->my_key; $my_value = $session['my_key']; // Set variable value $app->get('session')->set('my_key', 'my_value'); $session->my_key = 'my_value'; $session['my_key'] = 'my_value'; // Merge value recursively $app->get('session')->merge('my_key', ['first' => 'value']); $session->merge('my_key', ['second' => ['a' => 'A']]); $letter_a = $session['my_key']['second']['a']; // "A" // Delete variable $session->delete('my_key'); unset($session->my_key); unset($session['my_key']); // Destroy session $session::destroy(); // Get session id $id = $this->session::id(); return $res; });
Contributors
Here are the big ones listed. 😄
TODO
- Complete
Helpertests. (thanks @Zemistr) - Slim-specific tests (integration with Slim App).
License
MIT
统计信息
- 总下载量: 927.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 241
- 点击次数: 1
- 依赖项目数: 22
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-26