请提供更多代码,以便我们准确找到您的问题所在。
我假设您在 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"