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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community