如何修复 Slim 3 PUT、DELETE 请求不起作用

How to fix Slim 3 PUT,DELETE request not working

我正在尝试执行 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

我是不是漏掉了什么?谢谢

我通过如下添加隐藏输入法设法解决了我的问题。

//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');
});

试试,

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