php - 如何使用 Laravel 发出删除请求

标签 php html laravel web

我没有使用资源 Controller 。

路线:

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

Controller 函数:

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

电话:

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

程序返回 MethodNotAllowedHttpException。

谢谢。

最佳答案

你可以试试这个(注意隐藏的 _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>

检查 Form Method Spoofing .

更新:

在最新版本的 Laravel 中,可以在表单中为 csrfmethod 使用 blade 指令,例如:

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

关于php - 如何使用 Laravel 发出删除请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888433/

相关文章:

php - 拉维尔 5 : forget cache not working

PHP - session 标识符 - 安全

php - 结果和时间表

html - 继承 CSS 如何阻止它?

javascript - 从 Canvas 转换后如何将图像作为表单的一部分提交?

php - 找不到带点(IP地址)的路由,返回404

php - 遍历json数组并插入mysql php

php - 由空格/逗号分隔的标签的正则表达式,最多 n 个标签

javascript - react 事件处理程序未在 Docusaurus 页面上执行

laravel - 无法使用 SendGrid 中的 2 个可能的身份验证器使用用户名 "apikey"在 SMTP 服务器上进行身份验证