如何使用 Laravel 发出删除请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44888433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:15:02  来源:igfitidea点击:

How to make a delete request with Laravel

phphtmllaravelweb

提问by rur2641

I am not using resource controller.

我没有使用资源控制器。

The route:

路线:

Route::delete('/deleteTag/{tag}','Controller2@deleteTag');

The controller function:

控制器功能:

public function deleteTag(Tag $tag){
  $Tag = Tag::where('id', $tag->id)->get()->first();
  $Tag->delete();
  return redirect()->action('Controller2@main');
}

The call:

电话:

<form method="delete" action="http://***/public/deleteTag/{{$tag->id}}"> 
    {!! Form::token() !!} 
    <button type="submit">delete</button>
</form>

The program returns a MethodNotAllowedHttpException.

程序返回 MethodNotAllowedHttpException。

Thank you.

谢谢你。

回答by The Alpha

You may try this (Notice the hidden _methodinput):

你可以试试这个(注意隐藏的_method输入):

<form method="post" action="http://***/public/deleteTag/{{$tag->id}}"> 
    {!! Form::token() !!}
    <input type="hidden" name="_method" value="DELETE">
    <button type="submit">delete</button>
</form>

Check Form Method Spoofing.

检查表单方法欺骗

Update:

更新:

In the latest versions of Laravel, it's possible to use blade directives for csrfand methodin the form, for example:

在Laravel的最新版本,它可以使用刀片指令用于csrfmethod形式,例如:

<form method="post" action="..."> 
    @csrf
    @method('DELETE')
    <button type="submit">delete</button>
</form>