我试图在一个不返回响应的函数中设置cookie。从中间件调用该函数。
中间件:// Verify user
$userId = $userService->verify($email, $key);
if ($userId) {
$userService->authenticate($userId, $key);
return $next($request);
} else {
return response('Unauthorized', 401);
}
正在调用$userService->authenticate:
// Get user
$user = $this->getById($id);
// Get user zipcode
$user->zipcode = $this->getZipcodeById($id);
// Set session
Session::set('user', $user);
if (!Cookie::has('user_forever')) {
// SET COOKIE
}
我试图在身份验证中返回一个响应,但它没有工作。我不能让它工作…所以我感谢所有的帮助!
Thanks in advance
您可以使用queue()
作为:
Cookie::queue('key', 'value', 10);
根据docs,你可以使用cookie助手生成一个cookie实例,
$cookie = cookie('name', 'value', $minutes);
但是它不会被发送回客户端,除非它被附加到一个响应实例。尝试从下面的中间件发送
$response = $next($request);
return $response->cookie($cookie);
未测试的代码