我正在學習Laravel和apiResource. 我的頁面回傳 404。我需要我的第二個路徑是一個變數,因為它是顯示產品的子類別,因此它需要根據從 productId 傳遞的 ID 進行深入分析。
/products - shows the parent products (ie.transport)
/products/{id} - shows (ie.cars)
/products/{id}/{car-types} - shows (ie.sports-cars, luxury-cars, 4wd)
/products/{id}/{car-types}/{id} - shows (ie. the actual car)
路線
Route::apiResource('/products', ProductController::class);
Route::apiResource('/products.{product-types}', ProductTypeController::class);
uj5u.com熱心網友回復:
你可以做這樣的事情
路線
Route::apiResource('products', ProductController::class)
->only(['index', 'show'])
->parameters(['products' => 'id']);
Route::apiResource('products.product-types', ProductTypeController::class)
->only(['index', 'show'])
->parameters([
'products' => 'pid',
'product-types' => 'id',
]);
網址
/products
/products/{id}
/products/{pid}/product-types
/products/{pid}/product-types/{id}
uj5u.com熱心網友回復:
此處的錯誤是由于您在 name 引數中使用了花括號 ( {, })引起的apiResource,這些不是必需的(請查看#nested 資源檔案以獲取更多資訊)。
因此,要解決您的問題,您需要做的就是洗掉花括號。
Route::apiResource('/products', ProductController::class);
Route::apiResource('/products.product-types', ProductTypeController::class);
這將導致生成標準化路線(php artisan route:list順便在終端中使用以獲取可用配置路線的輸出)。
GET|HEAD products ................................................................... products.index ? ProductController@index
POST products ................................................................... products.store ? ProductController@store
GET|HEAD products/{product} ........................................................... products.show ? ProductController@show
PUT|PATCH products/{product} ....................................................... products.update ? ProductController@update
DELETE products/{product} ..................................................... products.destroy ? ProductController@destroy
GET|HEAD products/{product}/product-types ......................... products.product-types.index ? ProductTypeController@index
POST products/{product}/product-types ......................... products.product-types.store ? ProductTypeController@store
GET|HEAD products/{product}/product-types/{product_type} ............ products.product-types.show ? ProductTypeController@show
PUT|PATCH products/{product}/product-types/{product_type} ........ products.product-types.update ? ProductTypeController@update
DELETE products/{product}/product-types/{product_type} ...... products.product-types.destroy ? ProductTypeController@destroy
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504333.html
上一篇:RESTAPI設計:更新時的路徑變數與請求正文(最佳實踐)
下一篇:在聊天應用程式中檢索訊息
