我希望能够根据从uri收集的数据来选择控制器。
我有一个类别表和一个子类别表。基本上,我有一个以下格式的网址(:any)/(:any)
。第一个通配符是一个城市slug (即edinburgh),第二个通配符是一个类别或子类别的slug。
因此,在我的路径中,我使用该路径搜索类别,如果我找到它,我想使用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
,但是我需要一种方法来区分第二个插件。
最后,可能还有其他URL正在使用(:any)/(:any),所以我需要它能够继续查找其他路由。
发布于 2013-01-17 20:34:04
按顺序回答您的问题:
controller#action
,您可以使用单个操作,并基于第二个插件(类别或子类别)呈现不同的视图(尽管我不喜欢这种方法,请参阅#2和#3):public class Forsale_Controller extends Base_Controller {
public function get_products($city, $category_slug) {
$category = Category::where_slug($category_slug)->first();
if($category) {
// Do whatever you want to do!
return View::make('forsale.category')->with(/* pass in your data */);
}
$subcategory = Subcategory::where_slug($category_slug)->first();
if($subcategory) {
// Do whatever you want to do!
return View::make('forsale.sub_category')->with(/* pass in your data */);
}
}
}
/city_slug/category_slug/subcategory_slug
比你的方法要好得多!你应该使用这个!!/products/city/category/subcategory
这样的东西要清晰得多!希望它能有所帮助(我的代码更像是一个psudocode,它没有经过测试)!
https://stackoverflow.com/questions/14378799
复制相似问题