Laravel Routing Tutorial
Routing is one of the most important parts of any Laravel application. It decides how your app responds when users visit a URL. Whether you’re building a blog, an API or a large web application understanding Laravel routes is absolutely very crucial.
In this tutorial, I will tell you everything about Laravel routing. From creating simple routes to advanced concepts like route parameters, route groups, named routes, route model binding and much more. I will also share some personal tips from my experience that can save you from common routing mistakes.
What Are Routes in Laravel?
A route is simply a path that connects a URL to a specific action (like showing a view or running a controller method).
When a user types https://example.com/about the Laravel router checks the routes you have defined and sends that request to the correct controller or closure.
All routes are stored inside the routes folder:
-
routes/web.php— for web routes (browser-based) -
routes/api.php— for API routes (JSON-based) -
routes/console.php— for Artisan console commands -
routes/channels.php— for broadcasting channels
This separation keeps your project clean and organized.
Creating Your First Route in Laravel
Let’s start simple. Open routes/web.php and add this:
Now visit http://127.0.0.1:8000/hello in your browser. You’ll see the text appear.
This is a basic GET route. Laravel also supports POST, PUT, PATCH, DELETE:
Route Parameters
Most of the time, we need dynamic routes that accept values like IDs or slugs. This is where route parameters come in.
Example of required route parameter:
If you visit /user/10, it will print User ID: 10.
Example of optional route parameter:
If you open /user, it shows “Hello Guest”. If you open /user/Ahmad, it shows “Hello Ahmad”.
💡 Tip: Always give default values when using optional parameters to avoid “missing required parameters for route” errors.
Named Routes in Laravel
Sometimes you don’t want to hardcode URLs everywhere. Laravel allows you to name routes, which makes them easier to manage.
Then you can generate the URL anywhere like this:
$url = route('dashboard');
return redirect()->route('dashboard');
Benefits of using Named routes:
-
Easy to refactor URLs
-
Avoids broken links when you change route paths
-
Useful in redirects, Blade templates, and controllers
Route Groups and Middleware
When you have a bunch of routes that share the same prefix or middleware, grouping them is cleaner.
Example:
Route::middleware(['auth'])->prefix('admin')->group(function () {
Route::get('/dashboard', [AdminController::class, 'dashboard']);
Route::get('/users', [AdminController::class, 'users']);
});
-
Now both routes will have
admin/in front. -
Both require user authentication (
authmiddleware).
This is super useful when building admin panels or APIs.
Web Routes vs API Routes
Laravel differentiate routes into web and api types:
| Web Routes | API Routes |
Defined in web.php |
Defined in api.php |
| Uses session, cookies, CSRF | Stateless (no session) |
| Returns HTML responses | Returns JSON responses |
Use web routes for pages, dashboards, blogs.
-
Use api routes for mobile apps, React Native apps, or SPA frontends.
💡 If you want to connect a React Native frontend, define your routes in api.php and return JSON data. The React app will consume this via HTTP requests.
Route Model Binding (Automatic Model Injection)
Typing User::find($id) in every controller is boring. Laravel solves this with route model binding.
Example:
If you go to /user/1, Laravel automatically fetches the user with ID 1.
You can also bind using slug or custom field:
Then:
This is perfect for SEO-friendly URLs like /post/my-first-blog.
Managing Routes — Useful Commands
Some handy Artisan commands:
php artisan route:list # shows all routes
php artisan route:cache # caches your routes for speed
php artisan route:clear # clears route cache
Adding New Route Files
In large projects, web.php can become messy. Laravel lets you create custom route files and register them manually.
Example: create a file routes/admin.php and add:
Then in RouteServiceProvider.php:
This keeps your routes clean and organized.
Route Redirects
Sometimes you want to redirect one route to another:
Or:
This is useful if you rename URLs or move pages around.
Common Routing Mistakes (and Fixes)
-
404 route not found: Make sure route is defined above
fallbackroute and your server points topublic/index.php. -
Missing required parameters: Always check your route URL and controller method parameters.
-
Route not updating: Run
php artisan route:clearto reset cache.
❓ Laravel Routing FAQs
Q1: How to define a route in Laravel?
Use Route::get('/path', callback) inside routes/web.php.
Q2: How to create API route in Laravel?
Use Route::get('/api-endpoint', Controller@method) in routes/api.php.
Q3: How to register a new route file in Laravel 11 or 12?
Create a new file in /routes and register it in RouteServiceProvider.
Q4: How to get current route name in Laravel?
Use Route::currentRouteName().
Q5: How to get route parameters in Laravel?
Use $request->route('parameterName') or method injection.
Q6: How to clear route cache in Laravel?
Run php artisan route:clear in your terminal.
Q7: What is reverse routing in Laravel?
It means generating URLs from route names instead of hardcoding URLs.
Q8: How to pass multiple parameters in a route?
Q9: How to use route model binding with relationships?
You can eager-load related models inside the bound model’s booted() method or controller method.
Q10: Laravel route model binding not working?
Check if your parameter name matches the route segment and the model exists.
Summary
Routing is the backbone of every Laravel app. Once you master it, everything else—like controllers, views, authentication becomes much easier.
-
Start with basic routes
-
Move to parameters and named routes
-
Use route groups and middleware to organize
-
Prefer route model binding for clean code
-
Keep routes organized using separate route files
Learning routing well saves you time and prevents bugs later on. Practice it. try building a few pages, APIs, and dynamic URLs until it feels natural.