Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookie without response in "laravel"

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');
});
like image 239
user3409565 Avatar asked Mar 12 '14 08:03

user3409565


People also ask

How to Set Cookie for queue in Laravel?

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.

What is the Secure attribute in Laravel?

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.

Can I add a cookie to an outgoing response?

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:

How to retrieve a cookie from a HTTP request?

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.


1 Answers

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

like image 137
Needpoule Avatar answered Nov 09 '22 18:11

Needpoule