laravel发送put请求主要有两种
准备一个路由
Route::put('url地址', function(){
dump($_SERVER);
return 'this is put';
});
- form表单伪造put请求
并非是真正的put请求,通过dump($_SERVER);可以看到REQUEST_METHOD键名里面内容是post,只不过laravel通过识别他的_method字段知道需要把它当成put请求
<form action="url地址" method="post">
<input type="text" value="你好" name="tt">
{{ csrf_field() }}
// laravel当然有类似{{ csrf_field()}}的写法,不过最后仍然会转换成input标签写法。
<input type="hidden" name="_method" value="PUT">
<button type="submit">确定</button>
</form>
- ajax发送put请求
真正的put请求,通过dump($_SERVER);可以看到REQUEST_METHOD键名里面内容是put
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status==200) {
console.log('hello,world');
}
}
xhr.open("PUT", "url地址", true);
xhr.send();
- delete请求和patch请求自然都是向上面一样。但是我们想要伪造delete请求的时候不用表单,只想要get方式伪造delete请求怎么做
修改app\Providers\AppServiceProvider.php的boot函数
public function boot()
{
// 先打印一下原来的,查看method
dump(request());
// 从get请求里面获取有没有_method字段值
$method=strtoupper(request()->input('_method','get'));
// 判断_method字段值是否是符合规则的
if(in_array($method,['GET','POST','PATCH','PUT','DELETE'])){
// 重定义请求方式
request()->setMethod($method);
}
// 再次打印,查看修改后的method
dump(request());
}