I think you are calling this function after the page is initialized. So no cookie headers can be sent after the html tags started.
Refer to this Link
and this will help you
题
I am trying to set a cookie in an ajax call, but can't get it to work. I have understood that I need to set it via setCookie on $response->headers. The $cookie variable looks correct, but no cookie is set in the browser.
Code from my controller:
$cookie = Cookie::make('testcookie', array('testvalue' => 'test'), 1000);
$response = Response::json(array('status' => 'ok'));
$response->headers->setCookie($cookie);
return $response;