calin.ionut
10 months ago

dynamic routes

Posted 10 months ago by calin.ionut

I want to have a Controller (DynamicController) and inside a method that handles all dynamic routes.

public function handle($categorySlug, $filters = null)
    {
        $controller = HomeController::class;
        $action = 'index';

        // just for testing :)
        if ($categorySlug == 'something') {
            $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);
    }

the route that i defined is the last one in routes:

Route::get('/{categorySlug}/{filters}', '[email protected]')->where(['filters' => '.*']);

How to pass the $filters (or any other parameter) to "showCategory" method from the handle?

i want to create dynamic url's for product, category and brand:

ex:

/{categorySlug}/{brandSlug}/{productSlug}

and for filters:

/{categorySlug}/filters/{filters}

Please sign in or create an account to participate in this conversation.