我可以设置cookie,但无法获取cookie,请帮忙。
use Cookie; // use cookie
...
public function index() {
$cookie = Cookie::forever('default_store', 1);
$default_store = Cookie::get('default_store');
print_r($cookie);
// return
Symfony'Component'HttpFoundation'Cookie Object (
[name:protected] => default_store
[value:protected] => 1
[domain:protected] =>
[expire:protected] => 1618241894
[path:protected] => /
[secure:protected] => [httpOnly:protected] => 1 )
print_r($default_store);
// return empty
}
如果这是Laravel 5,请查看请求部分中关于使用Cookie的文档:https://laravel.com/docs/master/requests#cookies
看起来有两种可能性:
- 你没有使用请求,所以你根本没有从页面上获得cookie,但你仍然成功地设置了它
- 此页面的路由可能未使用"web"中间件
试试这些,看看会发生什么。这可能是非常简单的事情。
如果您使用的是旧版本,文档是一个很好的起点。