在Laravel 4中使用Cookie [英] Using Cookies in Laravel 4

查看:108
本文介绍了在Laravel 4中使用Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Laravel 4中使用Cookie?

How do you use cookies in Laravel 4?

我确定它很简单,只是不是与我点击,但我需要一点帮助。

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

根据我可以告诉,你必须创建一个这样的cookie:

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

$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?

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

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()?

像我说的,我可能只是缺少一些东西,在实际的方式...

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,您应该将您的视图添加到Response对象,并返回整个事件。例如:

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

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

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

是的,这还有点多。推理是视图和响应是两个独立的东西。您可以使用Views来解析各种用途的内容和数据,而不一定是发送到浏览器。这就是Response是什么,为什么如果你想设置头,Cookie或这种性质的东西,它是通过Response对象。

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.

这篇关于在Laravel 4中使用Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆