在 Laravel Octane / Swoole 中设置 cookie

Setting cookies in Laravel Octane / Swoole

我正在尝试在 Laravel Octane 上设置 cookie,但是这些 cookie 似乎没有在给定索引上定义。

setcookie(self::JWT_TOKEN_COOKIE, $token_details['access_token'], [
         'httponly' => true,
         'expires' => time() + $token_details['expires_in'],
         'path' => '/'
]);

die($_COOKIE[self::JWT_TOKEN_COOKIE]);

尝试打印 Cookie 时,我收到一条错误消息,告诉我它尚未设置。

Undefined array key "JWT_TOKEN"

我在 Laravel Octane 中设置 cookie 的方式有问题吗?我应该使用新的 class 来使用这些应用程序,因为该应用程序现在已完全保留在 RAM/loaded 中一次?

setcookie函数添加Set-Cookieheader

超全局数组$_COOKIE读取自Cookieheader

删除die功能并重新加载页面,cookie会出现

例子

<?php

setcookie('a', 'b', ['httponly' => true, 'expires' => time() + 30, 'path' => '/']);

print_r($_COOKIE);

请求将 cookie 保存到 tmp 文件的脚本

curl http://localhost:8000/ -c /tmp/cookies.txt --verbose
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
<---------- setcookie ---------->
< Set-Cookie: a=b; expires=Sun, 27-Mar-2022 23:50:36 GMT; Max-Age=30; path=/; HttpOnly
< 
<---------- print_r ---------->
Array 
(
)
* Closing connection 0

使用保存的 cookie 请求脚本

curl http://localhost:8000/ -b /tmp/cookies.txt --verbose
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.68.0
> Accept: */*
<---------- readed from /tmp/cookies.txt ---------->
> Cookie: a=b
> 
<---------- setcookie ---------->
* Replaced cookie a="b" for domain localhost, path /, expire 1648425170
< Set-Cookie: a=b; expires=Sun, 27-Mar-2022 23:52:51 GMT; Max-Age=30; path=/; HttpOnly
< 
<---------- print_r ---------->
Array
(
    [a] => b
)
* Closing connection 0

使用laravel响应object发送cookie

https://laravel.com/docs/9.x/responses#attaching-cookies-to-responses