Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tnort's avatar
Level 4

Laravel routes controller namespace

Hi all,

I hope someone can help here :) I have an app where I would manage some University courses hence I have different routes as such:

  1. domain/courses -> which would hit CourseController method index - for students
  2. domain/admin/courses -> which should hit Admin/CoutseController method index - for admins

Below is how I got started defining the routes for Admins Route::prefix('admin')->name('admin.')->group(function () { Route::resource('/users', UserController::class)->middleware(['auth', 'isAdmin']); Route::resource('/levels', LevelController::class)->middleware(['auth', 'isAdmin']); });

But I don't get how to bind different Controllers that use the same names to specific routes :( (CourseController to answer domain/courses and Admin/CourseController to answer for domain/admin/courses).

From Larravel documentation I understood that I have to use the namespace method with my routes so that grouped routes are bind to the specific namespace however I get a binding error:

Illuminate\Contracts\Container\BindingResolutionException Target class [Admin\App\Http\Controllers\Admin\UserController] does not exist.

0 likes
4 replies
vincent15000's avatar
Level 63

Hello,

What I have done is to use different names for the controllers, for example : CourseController and AdminCourseController.

And then to declare them in the routes file :

use App\Http\Controllers\CourseController;
use App\Http\Controllers\Admin\AdminCourseController;

I haven't found any other way to do it.

1 like
Snapey's avatar

route points to controller.... thats all there is to it ?

1 like
martinbean's avatar

@tudosm What’s the problem? Create different routes, and create different controllers for the front- and back-end:

use App\Http\Controllers\CourseController;
use App\Http\Controllers\Admin\CourseController as AdminCourseController;

// Front-end route
Route::get('/course', [CourseController::index, 'index']);

// Back-end route
Route::get('/admin/courses', [AdminCourseController::class, 'index']);

But importing a shed-load of controllers, and aliasing half of them, is why I preferred the string-based syntax in Laravel versions below 8.x.

2 likes
tnort's avatar
Level 4

Hi all,

I managed to solve the problem by setting a null namespace which I assume scopes the routes within that group to the Admin prefix like so:

Route::group(['namespace' => '', 'prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['auth', 'isAdmin']], function() { Route::get('/dashboard', [AdminController::class, 'index']); Route::resource('/users', UserController::class); });

But I think I like more the idea of naming differently the controllers :)

Please or to participate in this conversation.