【问题标题】:Laravel (5.4) throws 404 on valid API routeLaravel (5.4) 在有效的 API 路由上抛出 404
【发布时间】:2019-04-25 00:17:11
【问题描述】:

我一直在尝试使用各种不同的建议来解决这个问题,但似乎没有任何效果,虽然我可能遗漏了一些非常明显的东西,但我只是不明白为什么我在路线上遇到 404 错误确实存在。

我有一个 CRM,除了其他选项外,它还允许用户删除“可交付”(附加到案例的文件),但由于某种原因,我在尝试执行它时总是得到 404。

这是 React 组件中的调用:

documentCollectionRepo.removeDeliverable(deliverable.id).then(showSuccess, showError);

deliverable.id 肯定存在,因为我已通过控制台将其注销。

这里是 repo 中的函数:

export function removeDeliverable(documentCollectionId) {
   return Api.remove(`document-collections/${documentCollectionId}`);
}

最后,这里是 API 路由文件中的路由:

Route::delete('document-collections/{documentCollectionId}', 'DocumentCollectionController@deleteDocumentCollection');

当我单击删除按钮运行相关功能时,网络选项卡显示以下错误(IP 隐藏在此引用中):

请求网址:http://192.168.50.52/api/v1/document-collections/3
请求方法:删除
状态码:404 未找到
远程地址: 192.168.xx.xx:80
推荐人策略:no-referrer-when-downgrade

deleteDocumentCollection() 函数肯定存在于 DocumentCollectionController.php 文件中,并且所有其他函数都有效(包括另一个删除路线) - 所以我不太明白为什么这条路线不起作用。对此的任何帮助将不胜感激。

编辑:

这里是控制器功能:

public function deleteDocumentCollection(DeleteDocumentCollectionRequest $req, $documentCollectionId)
{
    $this->documentCollectionRepository->delete($documentCollectionId);

    return response()->ok();
}

在回购中:

public function delete($fileId)
{
    $file = $this->getOne($fileId);

    $this->deleteUploadedFile($file);
    $this->deleteNotifications($file);
    $this->deleteNotes($file);

    $file->delete();
}

【问题讨论】:

  • 您能否也发布此路线所在的群组?你的 laravel 日志是否包含任何错误?
  • 所有路由都包裹在Route::group(['middleware' => ['auth', 'store-last-active']], function() {组中,不,日志中没有其他错误,只有404

标签: laravel http-status-code-404


【解决方案1】:

如果你的 Laravel 项目是在 Apache 上配置的,那么可能会出现这样的问题。

因为 Apache 不允许 DELETE 请求,所以响应会是 404。

在 Laravel 默认代码之后添加到 .htaccess 中:

<Limit DELETE>
  Order deny,allow
  Allow from all
</Limit>

这也是可能的情况。如果它仍然不起作用,请简要分享您的代码,以便我研究它。

谢谢。

【讨论】:

  • 实际上我使用的是 nginx,而且正如我在问题中所说的那样,所有其他删除请求都可以正常工作 - 实际上就是那个
  • 你能不能只分享deleteDocumentCollection()函数的代码?
  • 我已将它们添加到问题中
猜你喜欢
  • 1970-01-01
  • 2017-06-29
  • 2018-04-13
  • 2019-12-17
  • 2017-09-20
  • 2021-04-12
  • 2017-09-28
  • 2017-10-17
  • 1970-01-01
相关资源
最近更新 更多