Laravel Routing Basics with Web and API Routes
Summary:
Learn how to define web and API routes effectively in Laravel.
Routing is one of the fundamental features in Laravel, serving as the bridge between a URI and its associated logic. Understanding how to define web and API routes effectively is key for building robust Laravel applications. This guide will walk you through Laravel’s routing basics, focusing on both web and API routes.
Understanding Routing in Laravel
At its core, Laravel’s routing system allows you to map URL patterns to controllers or closures. This means you can handle requests and dispatch them to the appropriate logic within your application.
Laravel provides two primary route files:
routes/web.php
– For web routes, which usually need session state, CSRF protection, and cookies.routes/api.php
– For stateless API routes, typically returning JSON.
Let’s dive into each.
Setting Up Web Routes
Web routes are defined in routes/web.php
. These routes are assigned the web
middleware group, which includes essential features such as session state and CSRF protection.
Defining a Basic Web Route
// routes/web.php
use Illuminate\Support\Facades\Route;
Route::get('/home', function () {
return view('home');
});
This route responds to a GET request at /home
and returns a home
view.
Using Route Parameters
You can capture route parameters and pass them to your logic.
Route::get('/user/{id}', function ($id) {
return "User ID: " . $id;
});
Naming Routes
Naming routes is helpful for URL generation:
Route::get('/dashboard', function () {
// ...
})->name('dashboard');
Controller Routes
For better organization, point routes to controller methods:
use App\Http\Controllers\UserController;
Route::get('/user/profile', [UserController::class, 'profile']);
Introducing API Routes
API routes are defined in routes/api.php
. These are stateless and by default assigned the api
middleware group.
Basic API Route
// routes/api.php
use Illuminate\Support\Facades\Route;
Route::get('/users', function () {
return App\Models\User::all();
});
This route returns a JSON response with all users.
API Resource Routes
Laravel provides a quick way to generate commonly used routes for RESTful controllers:
use App\Http\Controllers\API\UserController;
Route::apiResource('users', UserController::class);
This single line replaces the need to define a separate route for each action (index
, show
, store
, update
, destroy
).
Route Versioning
To support API versioning, prefix your routes:
Route::prefix('v1')->group(function () {
Route::apiResource('users', UserController::class);
});
Key Differences Between Web and API Routes
Feature | Web Routes | API Routes |
---|---|---|
File | routes/web.php |
routes/api.php |
Middleware | web group |
api group |
Session/CSRF | Enabled | Disabled |
Returns | Views, redirects | JSON responses |
Use Case | Web apps, frontend | APIs, mobile |
Tips for Effective Laravel Routing
- Group routes with common prefixes or middleware using
Route::group()
. - Use route names for easier maintenance.
- Pass data via route parameters and validate them using route constraints.
- Leverage resource controllers for CRUD APIs to keep your code DRY.
- Document your API routes for clarity using route:list and third-party tools.
Conclusion
Defining web and API routes effectively in Laravel ensures your application stays organized, secure, and scalable. By separating concerns between web.php
and api.php
, leveraging route groups, and using Laravel’s built-in features, you can build both frontend interfaces and robust APIs efficiently.
Happy routing!