我希望能够根据从uri收集的数据选择控制器。动态路由
我有一个分类表和一个子分类表。基本上我有一个以下格式的网址(:any)/(:any)
。第一个通配符是一个城市塞尔维亚(即爱丁堡),第二个通配符将是一个类别或一个子类别。
因此,在我的路线中,我搜索具有该路线的类别,如果找到它,我想使用controller:forsale和method:get_category。如果它不是一个类别,我会查找子类别,如果我在那里找到它,我想使用controller:forsale和method:get_subcategory。如果它不是一个子类别,我想继续寻找其他路线。
Route::get('(:any)/(:any)', array('as'=>'city_category', function($city_slug, $category_slug){
// is it a category?
$category = Category::where_slug($category_slug)->first();
if($category) {
// redirect to controller/method
}
// is it a subcategory?
$subcategory = Subcategory::where_slug($category_slug)->first();
if($subcategory) {
// redirect to controller/method
}
// continue looking for other routes
}));
首先我不知道如何在这里调用控制器/方法,而无需实际重定向(因此再次更改URL)。
其次,这是甚至是这样做的最好方法吗?我开始使用/city_slug/category_slug/subcategory_slug
。但我只想显示city_slug/category|subcategory_slug
,但我需要一种方法来确定第二块slu is是什么。
最后,可能会有其他网址在使用中,后面跟着(:any)/(:any),所以我需要它能够继续查找其他路线。