无法在Laravel 4中设置Cookie [英] Can't set cookies in Laravel 4
问题描述
我使用的是最新版本的Laravel 4,无法设定Cookie:
Route :: get 'cookietest',function()
{
Cookie :: forever('forever','Success');
$ forever = Cookie :: get('forever');
Cookie :: make('temporary','Victory',5);
$ temporary = Cookie :: get('temporary');
return View :: make 'forever'=> $ forever,'temporary'=> $ temporary,'variableTest'=>'works'));
});
查看脚本:
@extends('layouts.master')
@section('content')
永远cookie:{{$ forever}}< br /
临时Cookie:{{$ temporary}}< br />
变量测试:{{$ variableTest}}
@stop
永远cookie:
临时cookie:
变量测试:工作
如果我刷新页面或在一个路由中创建cookie并尝试在另一个路由中访问它,这并不重要。我可以确认没有设置与上述操作的cookie。 Cookie'laravel_payload'和'laravel_session'以及'remember_ [HASH]'确实存在,我可以使用setcookie使用普通PHP设置cookie。
没有错误被抛出或记录在任何地方,我可以找到。我在本地运行Linux Mint,在我的服务器上运行Debian,都是用nginx和我在这两个地方有同样的问题。
> Cookie不打算像这样使用,它们设置为下一个请求,而不是当前请求。您必须手动将它们附加到响应中,如文档中所述。 p>
因此,此代码
Cookie :: forever值');
$ cookie = Cookie :: get('cookie');
将不会获得结果,因为在请求结束时未附加Cookie。
您可以尝试使用
Route :: get ('cookies','Success');
$ tempCookie = Cookie :: make('temporary', 'Victory',5);
return Response :: make() - > withCookie($ foreverCookie) - > withCookie($ tempCookie);
}
Route :: get('cookietest',function()
{
$ forever = Cookie :: get('forever');
$ temporary ='temporary';''''''''''''''''; $ temporary = Cookie :: get('temporary');
return View :: make('cookietest',array('forever'=& =>'works'));
});
然后首先访问 yoursite.local / cookieset
然后 yoursite.local / cookietest
才能看到它的工作方式,并将设置该Cookie。
I'm using the latest version of Laravel 4 and I can't set cookies:
Route::get('cookietest', function()
{
Cookie::forever('forever', 'Success');
$forever = Cookie::get('forever');
Cookie::make('temporary', 'Victory', 5);
$temporary = Cookie::get('temporary');
return View::make('cookietest', array('forever' => $forever, 'temporary' => $temporary, 'variableTest' => 'works'));
});
View script:
@extends('layouts.master')
@section('content')
Forever cookie: {{ $forever }} <br />
Temporary cookie: {{ $temporary }} <br />
Variable test: {{ $variableTest }}
@stop
Yields:
Forever cookie:
Temporary cookie:
Variable test: works
It doesn't matter if I refresh the page or create the cookies in one route and try to access them in another. I can confirm that no cookies are being set with the above operation. The cookies 'laravel_payload' and 'laravel_session' as well as 'remember_[HASH]' do exist and I can set cookies with regular PHP using setcookie.
No errors are thrown or logged anywhere that I can find. I'm running Linux Mint locally and Debian on my server, both with nginx and I have the same problem in both places.
Cookies are not meant to be used like this, they are set for the next-request, not for the current request. And you have to manually attach them to your Response, as stated in the documentation.
So this code
Cookie::forever('cookie', 'value');
$cookie = Cookie::get('cookie');
will get no result because the cookie is not attached at the end of the request.
You can try it by splitting it in two routes like
Route::get('cookieset', function()
{
$foreverCookie = Cookie::forever('forever', 'Success');
$tempCookie = Cookie::make('temporary', 'Victory', 5);
return Response::make()->withCookie($foreverCookie)->withCookie($tempCookie);
});
Route::get('cookietest', function()
{
$forever = Cookie::get('forever');
$temporary = Cookie::get('temporary');
return View::make('cookietest', array('forever' => $forever, 'temporary' => $temporary, 'variableTest' => 'works'));
});
then first access yoursite.local/cookieset
and then yoursite.local/cookietest
to see that it works this way and the cookie will be set.
这篇关于无法在Laravel 4中设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!