jeudi 29 juin 2017

Why withCookie() is not working?

I have ran into the problem of not being able to set cookie with Laravel 5.2, and the problem has been solved after I read this post. (So what I need is not a solution but an explanation.)

In short,

return view('welcome')->withCookie(cookie('test', 'test', 45000));

This doesn't work, and to make it work, either:

 public function index(CookieJar $cookieJar, Request $request)
 {
     if($request->referrer){
        $cookieJar->queue(cookie('referrer', $request->referrer, 45000));
     }

     return view('welcome');
 }

Or:

$response = new \Illuminate\Http\Response(view('welcome'));
$response->withCookie(cookie('referrer', $request->referrer, 45000));
return $response;

What annoys me is that the first code doesn't return any error while it does not set any cookie, so that means it actually do something valid (but not setting the cookie). What it actually does? And why the later 2 solutions work?



via Chebli Mohamed

1 commentaire:

  1. WordPress Development Company - Infotrench provides custom WordPress development services in UK, USA, Delhi, Noida.
    wordpress development company

    RépondreSupprimer