Support the ongoing development of →
Laravel Routing
Last updated 2 years ago.

The router component will not know what to use.

An option that I have done before in an older project is to have a general Controller that forwards the request to the correct place.

Example simple version:

class DivideController
    public function __invoke(Request $request, $slug)
        $category = Category::where('sef', $slug)->first();
        if ($category) {
            $controller = app(CategoryController::class);
            return $controller->show($request, $category);
        $post = Post::where('slug', $slug)->first();
        if ($post) {
            $controller = app(PostController::class);
            return $controller->show($request, $post);
        $profile = User::where('uuid', $slug)->first();
        if ($profile) {
            // we have the user route
            $controller = app(ProfileController::class);
            return $controller->show($request, $profile);
        // we found nothing

Extra route (before the other routes)

Route::get('/{slug}', [App\Http\Controllers\DivideController::class])->name('divide');

With adding the extra route before the other routes the request will be routed to the DivideController but in your application you can still use the different routes.


Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.