0

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!

Coffee
  • 2,063
  • 8
  • 24
  • 57

1 Answers1

0

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));
    }
}
Community
  • 1
  • 1
theMohammedA
  • 677
  • 7
  • 11
  • I'm using $this, because in my controller __construct function I have argument (CookieJar $cookieJar) { $this->cookieJar = $cookieJar;} and if I try your code I'll get an error of undefined property – Coffee Jul 19 '16 at 14:07
  • Did you add `use Illuminate\Cookie\CookieJar;`? and for `$request` it gets auto injected in your function arguments e.g. `function sth(Request $request)` and it doesn't belong to the controller. – theMohammedA Jul 19 '16 at 14:12
  • yup, I used Illuminate\blahblah – Coffee Jul 19 '16 at 14:28