如何从后端(laravel)到前端发送cookie?

时间:2018-10-06 05:03:10

标签: laravel rest cookies vue.js

我想发送Bearer令牌并将其保存为前端的cookie。

我这样传递cookie:

return response()->json([
    'remember_token' => $user->remember_token,
], 200)->withCookie(cookie('auth_token', 'random_token', 3600));

但是它根本没有设置cookie!请帮忙!

1 个答案:

答案 0 :(得分:1)

尝试一下:

$cookie = Cookie::make('auth_token', $user->remember_token, 3600);
$response = Response::json('remember_token' => $user->remember_token, 200);
$response->headers->setCookie($cookie);
return $response;

取自this answer