在Laravel 4中设置Cookies仅适用于App :: after()

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.

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