vendredi 2 octobre 2015

Attach a cookie to a Laravel request

I have routes for setting translation options, and I want to attach a cookie to the route response, so that it will be read by a middleware and set the appropriate language.. and when the user comes back, the middleware will also know that that user has that language.

This is the route example:

 Route::get('/br', function (Illuminate\Http\Request $request) {
    //$request->withCookie(cookie()->forever('locale', "pt-br"));
    // i want to attach a cookie here.
    return Redirect::to('/');
});

And this is my middleware, which reads the cookie and sets the locale:

 public function handle($request, Closure $next)
    {

        $locale = $request->cookie('locale') ? $request->cookie('locale') : \Cookie::queued('locale') ;

        if($locale)
        {
            \App::setLocale($locale);
        }


        return $next($request);

    }

My problem is, on the first part of the code, I can't really attach a cookie to the $request variable, it must attach to a response variable.. Any ideas on how I can achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire