无法在 Laravel 4 中设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15777824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:24:53  来源:igfitidea点击:

Can't set cookies in Laravel 4

cookieslaravellaravel-4

提问by Beau

I'm using the latest version of Laravel 4 and I can't set cookies:

我使用的是最新版本的 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('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.

我是否刷新页面或在一个路由中创建 cookie 并尝试在另一个路由中访问它们都没有关系。我可以确认通过上述操作没有设置 cookie。cookie 'laravel_payload' 和 'laravel_session' 以及 'remember_[HASH]' 确实存在,我可以使用 setcookie 使用常规 PHP 设置 cookie。

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.

在我能找到的任何地方都不会抛出或记录任何错误。我在本地运行 Linux Mint,在我的服务器上运行 Debian,两者都使用 nginx,我在两个地方都遇到了同样的问题。

回答by Antonio Frignani

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.

Cookie 不打算这样使用,它们是为下一个请求设置的,而不是为当前请求设置的。并且您必须手动将它们附加到您的响应中,如文档中所述。

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.

不会得到任何结果,因为在请求结束时没有附加 cookie。

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/cookiesetand then yoursite.local/cookietestto see that it works this way and the cookie will be set.

然后首先访问yoursite.local/cookieset,然后yoursite.local/cookietest查看它是否以这种方式工作,并且将设置 cookie。

回答by Igor Parra

In Laravel 4we get the expected cookie behavior with queue.

Laravel 4 中,我们使用queue.

// Set a cookie before a response has been created
Cookie::queue('key', 'value', 'minutes');

Example:

例子:

Cookie::queue('username', 'mojoman', 60 * 24 * 30); // 30 days

Warning: In Laravel 3use put(http://v3.golaravel.com/api/class-Laravel.Cookie.html#_put).

警告:在Laravel 3 中使用puthttp://v3.golaravel.com/api/class-Laravel.Cookie.html#_put)。

Example:

例子:

Cookie::put('username', 'mojoman', 60 * 24 * 30); // 30 days

回答by Bat Fung

The afterFilter can be used to set Cookie in the controller. Assuming the cookie is stored in a controller's class variable $cookie. In the controller's constructor, the following code will automatically insert the cookie into whatever view returned to client:

afterFilter 可用于在控制器中设置 Cookie。假设 cookie 存储在控制器的类变量 $cookie 中。在控制器的构造函数中,以下代码将自动将 cookie 插入返回给客户端的任何视图中:

public function __construct () {
    $cookie = &$this->cookie;
    $this->afterFilter(function ($route, $request, $response) use(&$cookie)  {
            if ($cookie) {
                $response->withCookie( $cookie );
            }
    });
}