【问题标题】:Laravel PUT routes return 404Laravel PUT 路由返回 404
【发布时间】:2021-10-25 02:28:01
【问题描述】:

我正在尝试更新数据库中的数据 但它返回错误 404 not found in postman

Route::put('/products/{id}','productController@update');

【问题讨论】:

  • 你设置了必要的cors header
  • 是的,我做了@AdityaThakur
  • 我可以看看你的中间件吗?
  • 你的意思是内核文件中的中间件吗? @AdityaThakur
  • 不,我的意思是你在哪里设置了 cors 标头,stackoverflow.com/questions/33076705/…

标签: laravel


【解决方案1】:

只需在请求中添加 'Accept: application/json' 标头即可。

【讨论】:

    【解决方案2】:

    请提供更多代码,以便我们准确找到您的问题所在。

    我假设您在 api.php 文件中而不是在 web.php 文件中编写了路由。

    如果你这样做,你必须输入路由api/products/1

    如果您使用它,您还必须注意路由组前缀。组前缀中的每个路由都将被包装,并且每次您想要访问它时都需要在开头的前缀字符串。

    例如:

    web.php 文件中:

    Route::group(['prefix' => 'api'], function () {
        Route::put('products/{id}', 'productController@update');
    });
    
    # this will require you to access the url by tyiping "api/products/1".
    

    api php 文件中(新用户更需要注意这一点):

    Route::group(['prefix' => 'api'], function () {
        Route::put('products/{id}', 'productController@update');
    });
    
    # this will require you to access the url by tyiping "api/api/products/1" since the api.php file will automatically wrap your routes within an api prefix.
    

    还有一件事你需要知道,如果你在你的模型上使用getRoutesKeyName 方法,你应该根据你在方法中输入的内容,按照通配符使用id或者slug。

    例如:

    public function getRoutesKeyName(){
        return 'slug';
    }
    # this will require you to type "products/name-of-product" instead of "products/1"
    

    【讨论】:

    • 不需要添加['prefix' => 'api'],如果路由在api.php中列出,/api会自动添加到这些路由中,
    • @AdityaThakur 这只是在您的 api 路由上出现 404 错误的示例。我不是说你需要添加前缀,只是要注意使用它。请仔细阅读。
    猜你喜欢
    • 2021-07-13
    • 2021-01-31
    • 2014-07-12
    • 1970-01-01
    • 2018-02-11
    • 2015-07-01
    • 1970-01-01
    • 2019-04-20
    • 2020-08-26
    相关资源
    最近更新 更多