I want to set cookie after doing all stuffs with this group of route But when I use "before filter", it will return a response and then stop doing Another thing.
What should I do?
This is my code
Route::filter('setcookie',function() {
$test = Input::get('test',0);
$cookie = Cookie::forever('cookie',Input::get('test'));
return Response::make(View::make('pages.home'))->withCookie($cookie);
});
Route::group(array('before' => 'setcookie'),function()
{
Route::get('/', function() {
return View::make('pages.home');
});
Route::controller('productrest', 'ProductRestController');
Route::resource('product', 'ProductController');
});
You can use the Cookie::queue function of laravel. Route::filter ('cookie',function () { Cookie::queue ('key', 'value', 5); }); If you would like to set a cookie before a response has been created, use the Cookie::queue () method. The cookie will automatically be attached to the final response from your application.
The secure attribute is a value in the cookie structure that has to be set to true in order to ensure that the cookie is encrypted over HTTPS requests only. Modern versions of Laravel (tested with Laravel 8+) offers two simple ways to add that secure attribute to your cookies.
This holds the last user retrieved by credentials, which could not have been authenticated potentially, attempt returned false. You do not have to directly be adding a cookie to the Response. In the Cookie section of the docs should be information about "queue"ing a cookie to automatically be attached to the outgoing Response:
Once we set the cookie, we can retrieve the cookie by cookie () method. This cookie () method will take only one argument which will be the name of the cookie. The cookie method can be called by using the instance of Illuminate\Http\Request. Here is a sample code.
You can use the Cookie::queue
function of laravel.
Route::filter('cookie',function(){
Cookie::queue('key', 'value', 5);
});
From Laravel doc :
Queueing A Cookie For The Next Response
If you would like to set a cookie before a response has been created, use the Cookie::queue() method. The cookie will automatically be attached to the final response from your application.
http://laravel.com/docs/requests#cookies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With