在hyperf裏面使用session的時候可以先安裝組件包
composer require hyperf/session
Session 組件的配置儲存於 config/autoload/session.php 文件中
如文件不存在,可通過 php bin/hyperf.php vendor:publish hyperf/session
命令來將 Session 組件的配置文件發佈到 Skeleton 去。
修改這個文件
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact [email protected] * @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 [email protected] * @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等方法,可以看看手冊
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact [email protected] * @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配置問題,之前是/,修改好了之後就可以了.