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

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
        abort(404);
    }
}

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.