This article describes the form request type and CSRF protection in the laravel framework. Share it for your reference, as follows:
Laravel provides us with functions that bind different http request types.
Route::get('/test', function () {}); Route::post('/test', function () {}); Route::put('/test', function () {}); Route::patch('/test', function () {}); Route::delete('/test', function () {}); Route::options('/test', function () {});
But sometimes, we create a resource controller, and the update() method inside is bound to a PUT type http request.
This requires us to submit a simulated PUT request through the form. We can add a hidden field of _method with the value PUT.
<form action="{{ route('test') }}" method="post"> <input type="hidden" name="_method" value="PUT"> username:<input type="text" name="name"> password:<input type="password" name="pwd"> <input type="submit" value="submit"> </form>
You can also use the method_field() method provided by laravel for us.
<form action="{{ route('test') }}" method="post"> {{ method_field('PUT') }} username:<input type="text" name="name"> password:<input type="password" name="pwd"> <input type="submit" value="submit"> </form>
By default, laravel will verify the csrf token for each submission request. To pass the verification, you need to add the _token hidden field in the form.
<form action="{{ route('test') }}" method="post"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> username:<input type="text" name="name"> password:<input type="password" name="pwd"> <input type="submit" value="submit"> </form>
Or use the csrf_field() method.
<form action="{{ route('test') }}" method="post"> {{ csrf_field() }} username:<input type="text" name="name"> password:<input type="password" name="pwd"> <input type="submit" value="submit"> </form>
For more information about Laravel, readers who are interested in this site can view the topic:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.