Now, in the Routes, we use the entire namespace to call the Controllers. We can shorten that in two ways.
First, we can do this.
routes/web.php:
use Illuminate\Support\Facades\Route;use App\Http\Controllers\ProfileController;use App\Http\Controllers\Student\TimetableController; Route::get('/', function () { return view('welcome');}); Route::middleware(['auth', 'verified'])->group(function () { Route::prefix('student') ->middleware('role:1') ->name('student.') ->group(function () { Route::get('timetable', [\App\Http\Controllers\Student\TimetableController::class, 'index']) Route::get('timetable', [TimetableController::class, 'index']) ->name('timetable'); }); Route::prefix('teacher') ->middleware('role:2') ->name('teacher.') ->group(function () { Route::get('timetable', [\App\Http\Controllers\Teacher\TimetableController::class, 'index']) ->name('timetable'); });}); Route::middleware('auth')->group(function () { Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');}); require __DIR__ . '/auth.php';
But what to do with the Teacher Controller when the Controller names are both TimetableController
?
One way could be to use an alias.
routes/web.php:
use Illuminate\Support\Facades\Route;use App\Http\Controllers\ProfileController;use App\Http\Controllers\Student\TimetableController;use App\Http\Controllers\Teacher\TimetableController as TeacherTimetableController; Route::get('/', function () { return view('welcome');}); Route::middleware(['auth', 'verified'])->group(function () { Route::prefix('student') ->middleware('role:1') ->name('student.') ->group(function () { Route::get('timetable', [TimetableController::class, 'index']) ->name('timetable'); }); Route::prefix('teacher') ->middleware('role:2') ->name('teacher.') ->group(function () { Route::get('timetable', [\App\Http\Controllers\Teacher\TimetableController::class, 'index']) Route::get('timetable', [TeacherTimetableController::class, 'index']) ->name('timetable'); });}); Route::middleware('auth')->group(function () { Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');}); require __DIR__ . '/auth.php';
Another way is to use the namespace instead of a specific Controller. Then, when defining a Controller, we also use the last part of the namespace.
routes/web.php:
use Illuminate\Support\Facades\Route;use App\Http\Controllers\ProfileController;use App\Http\Controllers\Student\TimetableController; use App\Http\Controllers\Teacher\TimetableController as TeacherTimetableController;use App\Http\Controllers\Student; use App\Http\Controllers\Teacher; Route::get('/', function () { return view('welcome');}); Route::middleware(['auth', 'verified'])->group(function () { Route::prefix('student') ->middleware('role:1') ->name('student.') ->group(function () { Route::get('timetable', [TimetableController::class, 'index']) Route::get('timetable', [Student\TimetableController::class, 'index']) ->name('timetable'); }); Route::prefix('teacher') ->middleware('role:2') ->name('teacher.') ->group(function () { Route::get('timetable', [TeacherTimetableController::class, 'index']) Route::get('timetable', [Teacher\TimetableController::class, 'index']) ->name('timetable'); });}); Route::middleware('auth')->group(function () { Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');}); require __DIR__ . '/auth.php';
I believe the last example is the best approach. The references to the Controllers are much shorter, and there is no conflict between the Controllers with the same name.