Laravel PUT路线返回404

时间:2019-05-08 20:38:11

标签: laravel

我正在尝试更新数据库中的数据 但返回在邮递员中找不到的错误404

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

1 个答案:

答案 0 :(得分:0)

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

我假设您将路由写在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或根据方法内键入的内容使用id。

>

例如:

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