Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Cookies in Laravel 4

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.

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?

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...

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

like image 432
user165222 Avatar asked Sep 06 '13 20:09

user165222


People also ask

How do I enable cookies in laravel?

Cookie can be created by global cookie helper of Laravel. It is an instance of Symfony\Component\HttpFoundation\Cookie. The cookie can be attached to the response using the withCookie() method. Create a response instance of Illuminate\Http\Response class to call the withCookie() method.

How do you check if cookies are set or not in laravel?

Forum Check if cookie exists and is not null Last updated 3 months ago. You can get cookies from request: $value = Request::cookie('name', $defaultValue); if (Request::hasCookie('phone')) { ... }

What is session and cookies in laravel?

Laravel ships with several great drivers out of the box: file - sessions are stored in storage/framework/sessions . cookie - sessions are stored in secure, encrypted cookies. database - sessions are stored in a relational database. memcached / redis - sessions are stored in one of these fast, cache based stores.

How does PHP handle HTTP cookies?

Accessing Cookies with PHP Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will access all the cookies set in above example. You can use isset() function to check if a cookie is set or not.


4 Answers

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);

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.

like image 106
Aken Roberts Avatar answered Oct 04 '22 15:10

Aken Roberts


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

Cookie::queue('cookieName', 'cookieValue', $lifeTimeInMinutes);
like image 40
Antonio Carlos Ribeiro Avatar answered Oct 04 '22 15:10

Antonio Carlos Ribeiro


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

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

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

like image 24
Chris Goosey Avatar answered Oct 04 '22 13:10

Chris Goosey


You can also attach cookies to redirects like this

return Redirect::route('home')->withCookie($cookie);
like image 37
RiaanZA Avatar answered Oct 04 '22 13:10

RiaanZA