在 Laravel 4 中使用 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18665751/
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-14 08:23:29  来源:igfitidea点击:

Using Cookies in Laravel 4

phpcookieslaravellaravel-4

提问by user165222

How do you use cookies in Laravel 4?

你如何在 Laravel 4 中使用 cookie?

I'm sure it's simple and something just isn't clicking with me but I need a little help.

我敢肯定这很简单,有些东西并没有引起我的注意,但我需要一点帮助。

As far as I can tell, you have to create a cookie like this:

据我所知,您必须像这样创建一个 cookie:

$cookie = Cookie::make('test-cookie', 'test data', 30);

Then, aside from returning a custom response, how do you set it? What good is setting it with a custom response? When would I ever want to do this?

那么,除了返回自定义响应之外,您如何设置它?用自定义响应设置它有什么好处?我什么时候想要这样做?

What if I want to set a cookie and return a view? What good does return Response::make('some text')->withCookie('test-cookie')actually do me aside from showing me how to use withCookie()?

如果我想设置一个 cookie 并返回一个视图怎么办?return Response::make('some text')->withCookie('test-cookie')除了向我展示如何使用之外,实际上对我有什么好处withCookie()

Like I say, I'm probably just missing something here, but how would I use a cookie in a practical way...

就像我说的,我可能只是在这里遗漏了一些东西,但是我将如何以实用的方式使用 cookie...

...like somebody enters info, logs in, etc and I'd like to set a cookie and take them to a page made with a view?

...就像有人输入信息、登录等,我想设置一个 cookie 并将他们带到一个带有视图的页面?

回答by Aken Roberts

To return a cookie with a view, you should add your view to a Response object, and return the whole thing. For example:

要返回带有视图的 cookie,您应该将视图添加到 Response 对象,并返回整个内容。例如:

$view = View::make('categories.list')->with('categories', $categories);
$cookie = Cookie::make('test-cookie', 'test data', 30);

return Response::make($view)->withCookie($cookie);

Yeah, it's a little bit more to write. The reasoning is that Views and a Response are two separate things. You can use Views to parse content and data for various uses, not necessarily for sending to the browser. That's what Response is for, and why if you want to set headers, cookies, or things of that nature, it is done via the Response object.

是的,写的有点多。原因是 Views 和 Response 是两个不同的东西。您可以使用视图来解析内容和数据以用于各种用途,不一定要发送到浏览器。这就是 Response 的用途,以及为什么如果您想设置标题、cookie 或此类性质的东西,它是通过 Response 对象完成的。

回答by Antonio Carlos Ribeiro

This one is what I prefer to use: at any time, you can queue a cookie to be sent in the next request

这是我更喜欢使用的:在任何时候,您都可以将 cookie 排入队列以在下一个请求中发送

Cookie::queue('cookieName', 'cookieValue', $lifeTimeInMinutes);

回答by Chris Goosey

As described in the other answers, you can attach Cookies to Response/Views/Redirects simply enough.

如其他答案中所述,您可以简单地将 Cookie 附加到响应/视图/重定向。

$cookie = Cookie::make('name', 'value', 60);
$response = Response::make('Hello World');

return $response->withCookie($cookie);

or

或者

$cookie = Cookie::make('name', 'value', 60);
$view = View::make('categories.list');

return Response::make($view)->withCookie($cookie);

or

或者

$cookie = Cookie::make('name', 'value', 60);

return Redirect::route('home')->withCookie($cookie);

But you don't need to attach your Cookie to your response. Using Cookie:queue(), in the same way you would use Cookie::make(), your cookie will be included with the response when it is sent. No extra withCookie() method is needed.

但是您不需要将您的 Cookie 附加到您的回复中。使用 Cookie:queue(),与使用 Cookie::make() 的方式相同,您的 cookie 将在发送时包含在响应中。不需要额外的 withCookie() 方法。

Source: http://laravel.com/docs/requests#cookies

来源:http: //laravel.com/docs/requests#cookies

回答by RiaanZA

You can also attach cookies to redirects like this

您还可以将 cookie 附加到这样的重定向

return Redirect::route('home')->withCookie($cookie);