我想处理控制器中的所有动态路由,然后分派给正确的控制器。我正在使用laravel 5.7。
例如:对于我要设置路线的产品:
/{categorySlug}/{brandSlug}/{productSlug}
对于我想要的类别过滤器:
/{categorySlug}/filter/{filters}
我已经在route.php中定义了一条路由(最后一个):
Route::get('/{categorySlug}/{filters}', 'DynamicRouteController@handle')->where(['filters' => '.*']);
{filters}可以是/ filter1 / filter2 / filter3 / etc
DynamicRoute的句柄函数是:
public function handle($categorySlug, $filters = null)
{
$controller = HomeController::class;
$action = 'index';
if ($categorySlug == 'category-one') {
$controller = CategoryController::class;
$action = 'showCategory';
} else {
abort(404);
}
$container = app();
$route = $container->make(Route::class);
$controllerInstance = $container->make($controller);
return (new ControllerDispatcher($container))->dispatch($route, $controllerInstance, $action);
}
如果我使用路线:
/category-one/filter/filter-one/filter-two
将其分派到正确的控制器(CategoryController @ showCategory),然后从那里返回“一类”子弹类别的视图。
如何将一些参数传递给showCategory操作?
我需要将一些参数发送给CategoryController @ showCategory函数。