In Laravel, routing is the heart of your application. It determines how URLs map to controllers or closures and defines the behavior for every request. This guide walks you through the fundamentals of Laravel routing, step by step.
✅ Step 1: Routing Basics
In Laravel, a route is a URL path that leads to a specific logic or controller. For example, when a user visits yourapp.com/about
, Laravel needs to decide what to do — show a view, call a controller, or return JSON.
📌 Syntax Example:
✅ Step 2: Route Files
Laravel organizes its routes into different files:
routes/web.php
→ for browser-based (web) routesroutes/api.php
→ for API endpoints (JSON response)routes/console.php
→ for Artisan CLI routesroutes/channels.php
→ for broadcasting channels
📁 For most web apps, you’ll edit routes/web.php
.
✅ Step 3: Defining Routes
Laravel uses different HTTP verbs:
Route::get()
– Retrieve dataRoute::post()
– Submit dataRoute::put()
/Route::patch()
– Update dataRoute::delete()
– Delete data
Example:
✅ Step 4: Route Parameters
You can make routes dynamic using parameters:
You can also make them optional:
✅ Step 5: Route Naming
You can assign names to routes using the name()
method. This is useful for redirects or generating URLs.
✅ Step 6: Route Groups
Route groups allow you to group multiple routes under shared settings like middleware, prefix, or namespace.
Example: Group with prefix and middleware
✅ Step 7: Route Caching (Performance Boost)
When your app is ready for production, you can cache your routes for better performance:
To clear cached routes:
✅ Step 8: Controller Routes
Instead of defining logic in closures, Laravel promotes using controllers:
This will call the show()
method in UserController
.
✅ Step 9: Route Model Binding (Automatic Querying)
Laravel automatically binds route parameters to Eloquent models using Route Model Binding.
If you define the controller method like this:
Laravel will fetch the user record based on the ID in the URL.
🧠 Summary of Laravel Routing
Feature | Description |
---|---|
Route::get() |
Handles GET requests |
Route Parameters | Allows dynamic routes like /users/{id} |
Named Routes | Reference routes using names like route('home') |
Route Groups | Apply shared middleware or prefixes |
Controllers | Organize logic using controller classes |
Model Binding | Automatically resolve Eloquent models |
Route Caching | Speeds up routing in production |