Home >PHP Framework >Laravel >Notice! Laravel's pitfalls in deleting cookies

Notice! Laravel's pitfalls in deleting cookies

藏色散人
藏色散人forward
2020-07-24 14:28:493720browse

The following tutorial column from Laravel will introduce you to a small pitfall of deleting cookies in Laravel. I hope it will be helpful to friends who need it!

Notice! Laravel's pitfalls in deleting cookies

#Problem: Using Cookie::forget failed to properly delete cookies.

How to correctly delete the cookies of Laravel application?

In fact, there is a small pit here: when we delete Laravel Cookie, we must pay attention to returning the cookie deletion result!

$cookie = Cookie::forget('refreshToken');// 这里我们返回的时候要使用 withCookie !return response('view')->withCookie($cookie);

The above solution is suitable for application scenarios where the view is returned after deleting the cookie.

However, what if our request is an API request and JSON data is returned, for example:

Cookie::forget('refreshToken');return ['status' => true];

How do we delete Cookie at this time? In this case, even if we use withCookie, it is useless!

So for this application scenario, the final solution is as follows:

Cookie::queue(Cookie::forget('refreshToken'));return ['status' => true];

Use Cookie::queue to achieve the purpose .

In this way, the value of Laravel Cookie can be deleted correctly.

The above is the detailed content of Notice! Laravel's pitfalls in deleting cookies. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete