Session组件提供HTTP服务下的会话支持,Swoft实现了Redis驱动的Session存储支持,由于Swoft设计理念更倾向于分布式和集群,所以不建议使用文件的方式来进行会话存储。
依赖
- PHP 7.0 +
- Swoft Framework 1.0 beta +
- Swoole 2.0.11 +
安装
检查项目根目录下composer.json文件中是否已经安装swoft/session组件。
$ vim composer.json
requires:{
"swoft/session": "^1.0",
}
若没有安装则使用命令安装
$ composer require swoft/session
$ composer install
$ composer update
启用
将Swoft\Session\Middleware\SessionMiddleware::class中间件添加到全局中间件的配置文件config/beans/base.php中。
$ vim config/beans/base.php
'serverDispatcher' => [
'middlewares' => [
\Swoft\Session\Middleware\SessionMiddleware::class,
]
],
提前需配置swoft/redis组件的连接池,Redis驱动将会直接通过@Inject()注解注入\Swoft\Redis\Redis类用于操作。
安装Redis
对于Redis,首先需要在服务器上安装Redis服务器,若不是很清楚,可参见《Docker Redis》。
Docker安装配置swoft/redis
# 安装swoft/redis组件
$ composer require swoft/redis
$ composer install
$ composer update
# 配置全局环境.env
$ vim .env
# Redis
REDIS_NAME=redis
REDIS_DB=2
REDIS_URI=192.168.99.100:6379,192.168.99.100:6379
REDIS_MIN_ACTIVE=5
REDIS_MAX_ACTIVE=10
REDIS_MAX_WAIT=20
REDIS_MAX_WAIT_TIME=3
REDIS_MAX_IDLE_TIME=60
REDIS_TIMEOUT=3
REDIS_SERIALIZE=1
# 配置属性
$ vim config/properties/cache.php
<?php
return [
'redis' => [
'name' => 'redis',
'uri' => [
'192.168.99.100:6379',
'192.168.99.100:6379',
],
'minActive' => 8,
'maxActive' => 8,
'maxWait' => 8,
'maxWaitTime' => 3,
'maxIdleTime' => 60,
'timeout' => 8,
'db' => 1,
'prefix' => 'redis_',
'serialize' => 0,
],
'demoRedis' => [
'db' => 2,
'prefix' => 'demo_redis_',
],
];
配置
可通过SessionManager bean的配置实现对Session配置的管理,若Session配置不存在,首先需要先添加。
$ vim config/bean/base.php
'sessionManager' => [
'class' => \Swoft\Session\SessionManager::class,
'config' => [
'driver' => 'redis',
'name' => 'SWOFT_SESSION_ID',
'lifetime' => 1800,
'expire_on_close' => false,
'encrypt' => false,
'storage' => '@runtime/sessions',
],
],
配置参数
-
driverSession驱动类型,enum类型,可选值为redis|file
Session驱动类型目前仅支持将Redis驱动用于生产环境,file驱动尚未实现GC垃圾回收。 -
nameSession键名,string类型,对应到用户端存储Session信息的Cookies中的键名。 -
lifetimeSession生命时长单位为秒,int类型。 -
expire_on_close是否在请求结束时立即使当前Session过期,boolean类型。 -
encrypt是否Session中存储的内容进行加密,boolean类型。 -
storageSession信息存储的位置,仅在file驱动下有效,string类型。
使用
Swoft为Session提供了一个快捷方法session(),其文件位于/vendor/swoft/session/src/Helper/Functions.php,函数原型为
if (! function_exists('session')) {
/**
* Get Session
*
* @return \Swoft\Session\SessionInterface
*/
function session()
{
/** @var \Swoft\Session\SessionManager $sessionManager */
$sessionManager = \Swoft\App::getBean('sessionManager');
return $sessionManager->getSession();
}
}
由此可知session()函数实际是调用\Swoft\Session\SessionManager类的getSession()方法。
/**
* @return SessionInterface
*/
public function getSession(): SessionInterface
{
return RequestContext::getContextDataByKey(self::SESSION_KEY);
}
由于getSession()方法返回的是SessionInterface接口类型,重点关注SessionInterface接口对外暴露的接口。
-
session()->getName()获取Session会话的名称 -
session()->getId()获取当前Session会话的ID -
session()->all()获取所有Session会话的数据 -
session()->exists($key)检查一个键名对应的键值是否存在,返回布尔值。 -
session()->has($key)检查一个键名是否存在且不为空 -
session()->get($key, $default = null)根据键名获取键值,可设置默认值。 -
session()->put($key, $value = null)将一个或一组键值对存入Session -
session()->token()获取一个CSRF跨站请求伪造的令牌,表单中使用避免CSRF攻击。 -
session()->remove($key)根据键名删除键值并返回键值 -
session()->forget($keys)根据键名删除一个或多个键值 -
session()->flush()从Session会话中删除所有键值对
...
未完待续...