Home > Article > PHP Framework > Exception handling content of TokenMismatchException in laravel framework
laravel Under the framework, sometimes when requesting using methods such as post or put, a TokenMismatchException in VerifyCsrfToken.php line 67 error will sometimes be reported. The reason is that laravel has anti-CSRF turned on by default.
There are two ways to solve this problem, one is to submit the token value when requesting, and the other is to exclude the requested route when preventing CSRF
Method 1: Pass the token value
When submitting the form:
<form action="photo/12" method="post"> <?php echo method_field('PUT'); ?> <?php echo csrf_field(); ?> <input type="submit" name="提交" /> </form>
When using AJAX request:
<meta name="csrf-token" content="{{ csrf_token() }}">$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
Method 2: Exclude the specified URL from CSRF protection
For example, the accessed URL is http://laravel.com/photo/12, and now you want to exclude routes related to photo resources, then add the route in App\Http\Middleware\VerifyCsrfToken::class as follows:
protected $except = [ 'photo', 'photo/*', ];
Note that method two will not be able to protect photo-related routes from CSRF, so please choose according to the actual situation
The above is the entire content of this article. For more laravel content, please pay attention to laravel framework introductory tutorial.
Recommended related articles:
In-depth analysis of appearance mode in Laravel framework
vue.js in laravel5.4 framework Implementing Ajax form submission error verification
Related video recommendations:
The latest five Laravel video tutorial recommendations in 2017
The above is the detailed content of Exception handling content of TokenMismatchException in laravel framework. For more information, please follow other related articles on the PHP Chinese website!