Php 如何修复Slim 3 PUT、DELETE请求不起作用
我正在尝试实现删除和放置请求,但出现错误。下面是我的代码Php 如何修复Slim 3 PUT、DELETE请求不起作用,php,httprequest,slim-3,Php,Httprequest,Slim 3,我正在尝试实现删除和放置请求,但出现错误。下面是我的代码 $app->group('/departments', function () { $this->put('/{id}', DepartmentController::class . ':update')->setName('department.update'); $this->delete('/{id}', DepartmentController::class . ':destroy')->
$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>
试试看
您是如何调用API的?似乎您使用了一个
GET
,并且像您定义的那样,它只能管理PUT
和DELETE
http文本要更新数据,我有一个表单并提交给“department.update”路由名称是的,但是表单中使用的方法是什么?通常,它是一种形式上的POST
。是的,我使用POST
方法。我不知道问题出在哪里。你用部门/{id}
、是或放
($this put
)或删除
($this->delete
)来定义该URL,而不是发布
。因此您的表单无法执行,因为POST
的路由不存在。我的/routes/web.php
上已经有这些路由组。谢谢
$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');
});