【问题标题】:How to fix Slim 3 PUT,DELETE request not working如何修复 Slim 3 PUT、DELETE 请求不起作用
【发布时间】:2020-01-13 01:38:26
【问题描述】:

我正在尝试实现 DELETE 和 PUT 请求,但出现错误。下面是我的代码

$app->group('/departments', function () {
   $this->put('/{id}', DepartmentController::class . ':update')->setName('department.update');
   $this->delete('/{id}', DepartmentController::class . ':destroy')->setName('department.destroy');
});

当我尝试运行代码时收到此错误消息

Method not allowed. Must be one of: PUT, DELETE

我在这里错过了什么吗?谢谢

【问题讨论】:

  • 你是如何调用你的 API 的?看来您使用了GET,并且就像您定义的那样,它只能管理PUTDELETE http 动词
  • 更新数据,我有一个表格并提交到'department.update'路由名称
  • 是的,但是表单中使用的方法是什么?通常,它的形式是POST
  • 是的,我使用了post 方法。我不知道是什么问题。
  • 您使用departments/{id} 定义该URL,是或put ($this-put) 或delete ($this->delete) 但不是POST。所以你的表单无法执行,因为POST 的路由不存在

标签: php httprequest slim-3


【解决方案1】:

我设法通过添加隐藏输入法来解决我的问题,如下所示。

//Update
<form action="{{path_for('department.update', {'id':department.id})}}" method="post">
  <input type="hidden" name="_METHOD" value="PUT">
</form>

//Delete
<form action="{{path_for('department.destroy', {'id':department.id})}}" method="post">
  <input type="hidden" name="_METHOD" value="DELETE">
</form>

还有我的route

$app->group('/departments', function () {
   $app->put('/{id}', DepartmentController::class . ':update')->setName('department.update');
   $app->delete('/{id}', DepartmentController::class . ':destroy')->setName('department.destroy');
});

【讨论】:

    【解决方案2】:

    试试,

    $app->group('/departments', function () {
       $app->put('/{id}', DepartmentController::class . ':update')->setName('department.update');
       $app->delete('/{id}', DepartmentController::class . ':destroy')->setName('department.destroy');
    });
    

    【讨论】:

    • 我的/routes/web.php 已经有了这些路由组。谢谢
    猜你喜欢
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2021-03-23
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多