hyperf使用session
在hyperf里面使用session的时候可以先安装组件包
composer require hyperf/session
Session 组件的配置储存于 config/autoload/session.php 文件中
如文件不存在,可通过 php bin/hyperf.php vendor:publish hyperf/session
命令来将 Session 组件的配置文件发布到 Skeleton 去。
修改这个 config/autoload/middlewares.php 文件
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ return [ 'http' => [ \Hyperf\Session\Middleware\SessionMiddleware::class, ], ];
然后在 config/autoload/session.php 文件中
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ use Hyperf\Session\Handler; return [ 'handler' => Handler\FileHandler::class, 'options' => [ 'connection' => 'default', 'path' => BASE_PATH . '/runtime/session', 'gc_maxlifetime' => 1200, 'session_name' => 'HYPERF_SESSION_ID', 'domain' => null, 'cookie_lifetime' => 5 * 60 * 60, ], ];
之后别忘了在runtime下创建session文件夹
然后使用的时候可以这样做,可以类似于这样写用session成员变量来处理session,有常用的 has set get remove clear getId等方法,可以看看手册
我这里在父类里面写这个session,类似于这样
<?php class BaseController extends AbstractController { /** * @Inject() * @var \Hyperf\Contract\SessionInterface */ protected $session; }
然后子类继承父类
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ namespace App\Controller; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\Di\Annotation\Inject; use Hyperf\Contract\SessionInterface; /** * @AutoController(); */ class AdminLoginController extends BaseController { /** * @Inject() * @var \Hyperf\Contract\SessionInterface */ private $session; public function test_session() { $$user_info = ['user' => 'hello']; if (!$this->session->has('admin_info')) { $this->session->set('admin_info', json_decode(json_encode($user_info), true)); } $admin_info = $this->session->get('admin_info'); return [ 'admin_info' => $admin_info, ]; } }
实际上在使用的时候发现有个问题,http://local.hyperf.com访问的时候一直不断生成新的session,老的session拿不到,而http://127.0.0.1:9501就可以.
查了好多资料都不知道怎么描述的,结果翻到Nginx配置反向代理无法获取session才知道,是这样的,因为你的cookie设置和你的代码不是同一个目录导致无法读取到之前的数据,可以看一下这样配置proxy_cookie_path
# 至少需要一个 Hyperf 节点,多个配置多行 upstream hyperf { # Hyperf HTTP Server 的 IP 及 端口 server 127.0.0.1:9501; } server { # 监听端口 listen 80; # 绑定的域名,填写您的域名 server_name local.hyperf.com;
#这里是为了让静态资源被nginx快速返回 location ~ .*\.(gif|jpg|jpeg|png|js|css)$ { expires 11h; #proxy_pass http://127.0.0.1:9501; proxy_set_header hello $host; root /home/zhaoyao/script/php/hyperf/hyperf-skeleton/static/;#指定存放路径 } location / { valid_referers none blocked local.hyperf.com; if ($invalid_referer) { return 403; } # 将客户端的 Host 和 IP 信息一并转发到对应节点 proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 转发Cookie,设置 SameSite proxy_cookie_path /home/zhaoyao/script/php/hyperf/hyperf-skeleton/static/ "/; secure; HttpOnly; SameSite=strict"; # 执行代理访问真实服务器 proxy_pass http://127.0.0.1:9501; } }
原来是proxy_cookie_path配置问题,之前是/,修改好了之后就可以了.
分类:
hyperf
, hyperf-swoole php的性能神器
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析