Home  >  Article  >  PHP Framework  >  Exception handling content of TokenMismatchException in laravel framework

Exception handling content of TokenMismatchException in laravel framework

不言
不言Original
2018-07-31 15:27:353408browse

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(&#39;PUT&#39;); ?>
        <?php echo csrf_field(); ?>
        <input type="submit" name="提交" />
    </form>

When using AJAX request:

<meta name="csrf-token" content="{{ csrf_token() }}">$.ajaxSetup({
    headers: {        &#39;X-CSRF-TOKEN&#39;: $(&#39;meta[name="csrf-token"]&#39;).attr(&#39;content&#39;)
    }
});

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 = [        &#39;photo&#39;,        &#39;photo/*&#39;,
    ];

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!

Statement:
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