PHP › Laravel Framework Essentials

How does routing work in Laravel, and how do you define routes that map incoming URLs to specific controller actions?

Intermediate

Answer

Laravel routes are defined in dedicated route files, typically mapping a specific URL pattern and HTTP method to either a closure or, more commonly for larger applications, a specific controller method, and Laravel also supports route parameters that capture dynamic segments of a URL, such as a specific product identifier, automatically passing that captured value into the corresponding controller method as a parameter.
Route::get('/products/{id}', [ProductController::class, 'show']);
Real-world example An e commerce application defines a route mapping any URL matching the pattern slash products followed by a numeric identifier to the show method of its ProductController, automatically extracting that identifier and passing it directly into the method.

Common follow-ups: What is the difference between defining routes using a closure versus a controller method?;How do you group related routes together under a shared prefix or middleware?