Understand How Laravel Handles URLs, Requests, and Controllers
Learn how Laravel routing works and how to organize your application URLs properly.
Laravel's routing system is one of its most powerful and beginner-friendly features. Whether you're building a small blog or a full-scale SaaS app, understanding how Laravel routes work is essential for managing your application’s flow.
In this tutorial, you'll learn how to define, group, and customize routes using real-world examples. Perfect for Laravel 11 beginners. 🧭
What Are Routes in Laravel?
Routes are URL definitions that point to controllers, closures, or views. When a user visits a page in your app, Laravel checks the route to determine what logic to run.
1. Basic Route
Define a route in routes/web.php like this:
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
2. Route with Controller
Instead of using closures, you can direct routes to controller methods:
Route::get('/posts', [PostController::class, 'index']);
3. Route Parameters
Use route parameters to pass dynamic data:
Route::get('/posts/{id}', [PostController::class, 'show']);
In your controller:
public function show($id) {
return Post::findOrFail($id);
}
4. Named Routes
Naming routes makes them easier to reference:
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Use in views:
<a href="{{ route('dashboard') }}">Dashboard</a>
5. Route Groups
Group routes with middleware, prefixes, or namespaces:
Route::middleware(['auth'])->group(function () {
Route::get('/profile', [UserController::class, 'profile']);
Route::get('/settings', [UserController::class, 'settings']);
});
6. Resource Routes
Laravel can auto-generate all common CRUD routes for a resource:
Route::resource('posts', PostController::class);
This creates routes for `index`, `create`, `store`, `show`, `edit`, `update`, and `destroy`.
Final Thoughts
Understanding Laravel’s routing system unlocks a huge part of building efficient applications. From simple GET requests to complex route groups with middleware, mastering this system gives you complete control over your app’s URL structure.
Keep experimenting with routes, and try using route model binding, fallback routes, and API resource routing as you grow!