我想在Laravel 5中独立设置cookie
。,不要使用
return response($content)->withCookie(cookie('name', 'value'));
我只想在某个页面设置cookie并在其他页面检索
创建可以像这样
$cookie = Cookie::make('name', 'value', 60);
但是我如何在某些控制器中检索这些cookie呢?
你可以试试:
Cookie::queue($name, $value, $minutes);
这将使cookie排队,以便稍后使用它,然后当响应准备好发送时,它将与响应一起添加。您可以查看Laravel
网站上的文档。
Update (Retrieving A Cookie Value
):
$value = Cookie::get('name');
注意:如果您在当前请求中设置了cookie,那么您将能够在下一个后续请求中检索它
如果你想设置cookie并在请求之外获取它,Laravel不是你的朋友。
Laravel cookie是Request的一部分,所以如果你想在Request对象之外做这个,使用好'ole PHP setcookie(..)和$_COOKIE来获取它
你走对了,我的朋友。现在,如果您想在项目中的任何地方检索cookie
,只需将此代码$val = Cookie::get('COOKIE_NAME');
就是这样!更多信息请点击这里
下面是一个示例代码和解释。
//Create a response instance
$response = new Illuminate'Http'Response('Hello World');
//Call the withCookie() method with the response method
$response->withCookie(cookie('name', 'value', $minutes));
//return the response
return $response;
Cookie可以通过使用forever方法永久设置,如下面的代码所示。
$response->withCookie(cookie()->forever('name', 'value'));
检索Cookie
//’name’ is the name of the cookie to retrieve the value of
$value = $request->cookie('name');