hyperf会话session操作到redis

如何用hyperf来操作session

>安装

```
composer require hyperf/session
php bin/hyperf.php vendor:publish hyperf/session
```

>配置 Session 中间件

修改config/autoload/middlewares.php

```
[
\Hyperf\Session\Middleware\SessionMiddleware::class,
],
];
```

>配置储存驱动

修改config/autoload/session.php

```
Handler\RedisHandler::class, //改为文件则为FileHandler
'options' => [
'connection' => 'default',
'path' => BASE_PATH . '/runtime/session',
'gc_maxlifetime' => 1200,
'session_name' => 'HYPERF_SESSION_ID',
'domain' => null,
'cookie_lifetime' => 5 * 60 * 60,
],
];

```

创建controller文件

```
session 来使用
$sessionId = $this->session->getId();
//return $sessionId;
//$this->session->set('foo', 'bar');
//$data = $this->session->all();
return $this->session->get('foo');
}

}
```

然后打开redis

    A+
发布日期:2021年12月30日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: