phpcookiesrequestlaravel-5cookiejar

Cannot get queued cookie from request


So I have PHP controller that sends cookie in queue, and I need to get this cookie next time when I refresh the page (and call this controller).

When controller is called, it checks if the cookie exists in request, an if not, it sets it in queue with expiration time 15 minutes.

But when this controller is called again, nothing is got in request. I've looked in dev-tools->Network->Cookies and haven't found this cookie neither in Request nor in Response section. At the same time, getQueuedQookies() shows that this cookie has been added to the queue. The code looks as follows:

$cookie = $this->request->cookie('id');
if($cookie=='id') {
    die('ID detected.');
} else {
    $this->cookieJar->queue('id', 'id', 15);
}

Then other actions are taken, and the controller returns some string in the end.

What am I doing wrong and how can my problem be solved? Would highly appreciate any possible help!


Solution

  • Update

    change test to your function name and CookieJar will be auto injected.

    public function test(CookieJar $cookieJar, Request $request){
        $cookie= $request->cookie('id');
        if($cookie=='id') {
            print('ID detected.');
        } else {
            $cookieJar->queue(cookie('id', 'id', 15));
        }
    }