Php 多个控制器/页面的相同路由
我正在和拉威尔发展电子商务。以下是我的困惑: 这就是类别链接的外观: mysite.com/category_name mysite.com/category\u name[/probable\u sub\u category\u name] 这就是产品链接的外观: mysite.com/product_name mysite.com[/probable\u category\u name][/probable\u sub\u category\u name]/product\u name 我的路线设置如下:Php 多个控制器/页面的相同路由,php,routes,laravel,Php,Routes,Laravel,我正在和拉威尔发展电子商务。以下是我的困惑: 这就是类别链接的外观: mysite.com/category_name mysite.com/category\u name[/probable\u sub\u category\u name] 这就是产品链接的外观: mysite.com/product_name mysite.com[/probable\u category\u name][/probable\u sub\u category\u name]/product\u name 我的
Route::get(array('(:any)', '(:all)/(:any)'), array('before' => 'is_category', 'uses' => 'categories@index'));
Route::get(array('(:any)', '(:all)/(:any)'), array('before' => 'is_product', 'uses' => 'products@index'));
如果我把产品路线放在第一位,类别链接会断开,如果我把类别路线放在第一位,产品链接会断开,正如预期的那样。我如何对这两个应用程序使用相同的路由
我的PHP版本是5.3.10-1ubuntu3.4(说phpinfo())神奇的是Controller::call()方法。以下是解决方案:
Route::get(array('(:any)', '(:all)/(:any)'), function () {
// Current URI as array
$parameters = Request::route()->parameters;
// Checks if given parameter is a product's url value
$check_if_product = function ($parameter) {
$products = Product::where('url_title', '=', $parameter)->count('id');
return (is_numeric($products) && $products > 0 ? true : false);
};
// Checks if given parameter is a category's url value
$check_if_category = function ($parameter) {
$categories = Category::where('url_title', '=', $parameter)->count('id');
return (is_numeric($categories) && $categories > 0 ? true : false);
};
// If last parameter from URI belongs to a product
if ($check_if_product(end($parameters)))
{
return Controller::call('products@index');
}
// If last parameter from URI belongs to a category
elseif ($check_if_category(end($parameters)))
{
return Controller::call('categories@index');
}
else
{
return Response::error('404');
}
});
你应该将你的解决方案作为答案发布,然后在24小时后将其作为正确答案接受,以帮助清理未回答的问题。谢谢谢谢你,杰森。我希望我做得对。方法[call]不存在。代码是为Laravel 3编写的。我认为实现同样的功能变得越来越困难。