🧭 Route to Controller Mapping in Laravel
Instead of using closures in routes, Laravel encourages mapping your routes to controller methods to keep logic organized and reusable.
✅ Why Map Routes to Controllers?
- Keeps
routes/web.phpclean and readable - Separates logic from route definitions
- Improves testability and reuse
🛠 Basic Syntax
use App\Http\Controllers\PageController;
Route::get('/about', [PageController::class, 'about']);
This maps the /about route to PageController::about().
🧠 Controller Method
class PageController extends Controller
{
public function about() {
return view('about');
}
}
🔢 With Parameters
Route::get('/user/{id}', [UserController::class, 'show']);
$id will be passed to the show() method.
🧲 With Route Model Binding
Route::get('/user/{user}', [UserController::class, 'show']);
public function show(User $user) {
return $user->name;
}
☝️ Mapping to Invokable Controller
Use class name only (no method) if controller uses __invoke():
use App\Http\Controllers\ContactController;
Route::get('/contact', ContactController::class);
Internally calls ContactController::__invoke().
📚 Group Routes by Controller
Route::controller(PostController::class)->group(function () {
Route::get('/posts', 'index');
Route::get('/posts/{id}', 'show');
});
This avoids repeating the controller name in each route.
Route::resource() and Resource Controllers.


