Home > PHP Framework > Laravel > body text

Notice! Laravel's pitfalls in deleting cookies

藏色散人
Release: 2020-07-24 14:28:49
forward
3649 people have browsed it

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);
Copy after login

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];
Copy after login

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];
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!