ellipse / session
Psr-15 中间件,允许使用 Psr-7 请求和响应进行会话管理
0.3.0
2018-04-27 14:10 UTC
Requires
- php: >=7.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- eloquent/phony-kahlan: ^1.0
- kahlan/kahlan: ^4.0
- zendframework/zend-diactoros: ^1.7
This package is auto-updated.
Last update: 2024-08-26 00:01:59 UTC
README
本软件包提供了一个 Psr-15 中间件,用于管理会话。它使用默认的会话配置值即可直接使用,同时也适用于 Psr-7 请求/响应抽象。
要求 php >= 7.0
安装 composer require ellipse/session
运行测试 ./vendor/bin/kahlan
使用会话中间件
<?php use Ellipse\Dispatcher; use Ellipse\Session\SessionMiddleware; // By default SessionMiddleware uses php session configuration values. $middleware = new SessionMiddleware // Will call php session_set_save_handler($handler) before starting the session. ->withSaveHandler($handler) // Will call php session_name('session_name') before starting the session. ->withName('session_name') // Will call php session_save_path('/session/save/path') before starting the session. ->withSavePath('/session/save/path') // Will call php session_cache_limiter('public') before starting the session. ->withCacheLimiter('public') // Will call php session_cache_expire(60) before starting the session. ->withCacheExpire(60) // Will call php session_set_cookie_params(3600, '/path', 'domain', true, true) before starting the session. ->withCookieParams([ 'lifetime' => 3600, 'path' => '/path', 'domain' => 'domain', 'secure' => true, 'httponly' => true, ]); // Build a dispatcher using the session middleware. $dispatcher = new Dispatcher([ $middleware, // Next middleware have access to the request Ellipse\Session::class attribute. new class implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) { // Session data is attached to the Ellipse\Session::class request attribute. $session = $request->getAttribute(Ellipse\Session::class); // Return the session id (session_id()) $session->id(); // Regenerate the session id (session_regenerate_id(bool $delete_old_session = false)) $session->regenerate_id(); // Set a session value. $session->set('key', 'value'); // Set a session value only for the next session. $session->flash('key', 'value'); // Return whether a session value is set. $session->has('key'); // Return an array of all the session data. $session->all(); // Return the value associated to the given key. $session->get('key'); // Return a default value when the given key is not set. $session->get('notset', 'default'); // Unset a session value. $session->unset('key'); // Unset all session value. $session->delete(); } } ]);