0

I try to set a Cookie in Laravel 4 in a specific route.

Unfortunately, setting the Cookie does only work in the global App::after() filter.

First thing I tried was returning a response with a Cookiefrom my Controller.

This doesn't work:

return Response::make($view)->withCookie(Cookie::make('foo','bar'));

However, this does:

return Response::make()->withCookie(Cookie::make('foo','bar'));

But does not solve my problem.

Next I tried it with an after filter as follows.

Route::filter('cookie', function($route, $request, $response)
{
    $response->withCookie(Cookie::make('foo', 'bar'));
});

This does not work either.

Next, I tried it using Cookie::queue(), which I've found in another answer - with no luck.

The only place the Cookie is set properly is in App::after().

App::after(function($request, $response)
{
    $response->withCookie(Cookie::make('foo', 'bar'));
});

Besides I'm pretty sure that one of the other approaches should work, this solution doesn't give me the control I'm looking for.

I'm running Laravel v4.0.9.

Community
  • 1
  • 1
Rico Leuthold
  • 1,975
  • 5
  • 34
  • 49
  • How are you trying to access the cookie? Remember that cookies are not available until the **next** page load. That's just how cookies work, regardless of framework implementation. – Aken Roberts Nov 17 '13 at 00:23
  • I use the (Browser) Web Inspector to check if the cookie is set or not. – Rico Leuthold Nov 17 '13 at 07:02
  • I think the problem is when you set a cookie 'before', the response object is not yet created. Then in your controller, you create a 'new' Response::return() etc - so any previous values are overwritten. That is why it is working on your 'after' filter, because you are adding it to the created object. – Laurence Nov 17 '13 at 09:22
  • But then why doesn't it work in the custom after filter? There I attach the cookie to the response. Furthermore, it can be found in the documentation (http://four.laravel.com/docs/responses): `return Response::make($content)->withCookie($cookie);` and the queue approach is explained here: http://laravel.com/docs/requests#cookies – Rico Leuthold Nov 17 '13 at 16:41

1 Answers1

1

Try this tested, working code.
Specify expiration time (in minutes from now). Dont you use some cookie extension in your browser, which may protect/blacklist specified cookies from being modified...

Route::get('cookieset', function(){
    $cookie = Cookie::make('foo', 'bar', 60);
    return Redirect::to('cookieget')->withCookie($cookie);
});

Route::get('cookieget', function(){
    dd(Cookie::get('foo'));
});
Andreyco
  • 22,476
  • 5
  • 61
  • 65
  • This does work indeed. I implemented it the same way (using a redirect) at first. However, the redirect prevents the application from being loaded properly, when embedded in a Facebook page with a Fangate using IE9 (leads to a redirect loop). – Rico Leuthold Nov 18 '13 at 11:15
  • IE needs some little more effort to make it work in IE. Try to set this header `App::after(function($request, $response) { $response->headers->set('P3P','CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"'); });` – Andreyco Nov 18 '13 at 13:13
  • The problem is not IE specific and I already set the P3P header - although mine looks like this `$response->header('P3P', 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');` – Rico Leuthold Nov 18 '13 at 17:47