我正在尋找將我的路線換成電子商務商店以實現SEO目的。Laravel - 在routes.php中使用'where'來查詢模型
我想創建以下路線:
Route::get('/{country}', ['uses' => 'Store\[email protected]']);
Route::get('/{category}, ['uses' => 'Store\[email protected]']')
的country
和category
必須是動態的。
我想知道是否有類似以下內容的可能嗎?和實現的最佳途徑。
// Route 1
Route::get('/{country}', ['uses' => 'Store\[email protected]'])
->where('country', ProductCountry::select('slug')->get());
// Route 2
Route::get('/{category}', ['uses' => 'Store\[email protected]'])
->where('category', ProductCategory::select('slug')->get());
例如路線:
/great-britain should be routed via Route 1
/china should be routed via Route 1
/widgets should fail route 1, but be routed via Route 2 because
widgets are not in the product_country table but are in
the product_category table
我知道我可以寫死我的路線有可能的國家:
Route::get('/{country}', ['uses' => 'Store\[email protected]'])
->where('country', 'great-britain|china|japan|south-africa');
然而,這是笨拙和tedius。我想從數據庫中獲取國家列表。
肯定會在你的控制器處理不是在你的路線,你會不顧國家傳遞到你的控制器,但隨後的控制器會做你的國家==做xxx其他人做yyy等 – Dave
你可以使用Route :: model()來實現這一點。 – Anam
@Dave - 如果我想限制特定國家可用於控制器,我如何將路線傳遞到該國家?做你的方法,我的路線必須是'Route :: get('country'/ {country})'我不想要的。 – Gravy