Home > PHP Framework > Laravel > How to turn off csrf in laravel

How to turn off csrf in laravel

青灯夜游
Release: 2021-12-21 11:34:07
Original
2084 people have browsed it

Method: 1. Open the "Kernel.php" file under the app, find and comment out the "App\Http\Middleware\VerifyCsrfToken" code. 2. Open the "VerifyCsrfToken.PHP" file under the app and modify the "$except" attribute.

How to turn off csrf in laravel

The operating environment of this tutorial: Windows 7 system, Laravel 5 version, DELL G3 computer.

If you use laravel, you will know the csrf verification function. If there is no csrf_token when posting a value, the following error will be reported:

How to turn off csrf in laravel

This This is because in versions after 5.2, web middleware will be added to the routing by default.

There is the following configuration in the app/Http/Kernel.php file:

How to turn off csrf in laravel

Close all

At this point, everyone must know How to turn off this verification. Justcomment out the line of code marked in the picture above, which will turn off the csrf verification, but this will turn off everything.


Partially closed

When we write the interface, we will encounter such a problem: because csrf_token cannot be passed through the interface (csrf_token is generated in laravel), we I just want to turn off CSRF verification when making API requests, and the background of the website will not be turned off.

This requires modifying the app\Http\Middleware\VerifyCsrfToken.php file.

There is an attribute $except in the file. You can set which routes do not require csrf verification. What I want is as follows Routes starting with api do not perform CSRF verification. You only need to add 'api/*'.

How to turn off csrf in laravel

This way I visit http://***/laravel/public /api/index will not report an error.

Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of How to turn off csrf in laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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