How do you define routes in Laravel?
In Laravel, routes are defined in the routes
directory using several different files. Here's a comprehensive guide to defining routes:
Basic Route Files
Laravel uses several route files located in the routes/
directory:
web.php
- For web routes (with session, CSRF protection, etc.)api.php
- For API routes (with rate limiting, no session)console.php
- For Artisan console commandschannels.php
- For broadcasting channels
Basic Route Syntax
1. Simple GET Route
Route::get('/welcome', function () {
return 'Welcome to Laravel!';
});
2. Route with Controller Method
Route::get('/users', 'UserController@index');
// Or using PHP 8 syntax:
Route::get('/users', [UserController::class, 'index']);
3. Multiple HTTP Methods
Route::match(['get', 'post'], '/profile', function () {
// Handle both GET and POST requests
});
Route::any('/contact', function () {
// Handle any HTTP method
});
Route Parameters
1. Required Parameters
Route::get('/user/{id}', function ($id) {
return "User ID: " . $id;
});
2. Optional Parameters
Route::get('/user/{name?}', function ($name = 'Guest') {
return "Hello, " . $name;
});
3. Parameter Constraints
Route::get('/user/{id}', function ($id) {
// ...
})->where('id', '[0-9]+');
Route::get('/user/{name}', function ($name) {
// ...
})->where('name', '[A-Za-z]+');
Named Routes
Route::get('/user/profile', function () {
// ...
})->name('profile');
// Generate URL: route('profile')
// Redirect: redirect()->route('profile')
Route Groups
1. Prefix Groups
Route::prefix('admin')->group(function () {
Route::get('/users', function () {
// Matches "/admin/users"
});
Route::get('/settings', function () {
// Matches "/admin/settings"
});
});
2. Middleware Groups
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
// Requires authentication
});
});
3. Namespace Groups
Route::namespace('Admin')->group(function () {
// Controllers within "App\Http\Controllers\Admin"
Route::get('/admin/users', 'UserController@index');
});
Resource Routes (CRUD)
Route::resource('posts', 'PostController');
// Creates all CRUD routes automatically:
// GET /posts → index
// GET /posts/create → create
// POST /posts → store
// GET /posts/{post} → show
// GET /posts/{post}/edit → edit
// PUT/PATCH /posts/{post} → update
// DELETE /posts/{post} → destroy
Partial Resource Routes
Route::resource('posts', 'PostController')->only([
'index', 'show', 'create', 'store'
]);
Route::resource('posts', 'PostController')->except([
'destroy'
]);
API Resource Routes
Route::apiResource('posts', 'PostController');
// Creates only index, store, show, update, destroy (no create/edit)
Route Model Binding
1. Implicit Binding
Route::get('/user/{user}', function (User $user) {
return $user;
});
2. Custom Key
Route::get('/user/{user:email}', function (User $user) {
return $user;
});
Route Caching
For production performance, cache your routes:
php artisan route:cache
Clear the cache:
php artisan route:clear
Best Practices
- Use resource routes for CRUD operations
- Name your routes for easy URL generation
- Use middleware for authentication and authorization
- Group related routes together
- Use route model binding for cleaner code
- Cache routes in production for better performance
Example Structure
// routes/web.php
Route::get('/', 'HomeController@index')->name('home');
Route::middleware(['auth'])->group(function () {
Route::resource('posts', 'PostController');
Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
});
// routes/api.php
Route::prefix('v1')->group(function () {
Route::apiResource('users', 'UserController');
Route::post('/auth/login', 'AuthController@login');
});
This structure provides a clean, organized way to handle all your application's routes in Laravel.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.