🌐 API Routes vs Web Routes in Laravel
Laravel provides two main route files — web.php and api.php — each designed for specific types of applications. Understanding their differences is essential for building scalable apps.
📁 File Locations
Both route files are located in the routes/ directory:
- Web Routes:
routes/web.php - API Routes:
routes/api.php
⚖️ Key Differences
| Feature | web.php |
api.php |
|---|---|---|
| Route Prefix | None (e.g., /home) |
/api prefix automatically added (e.g., /api/users) |
| Middleware | Uses web middleware group |
Uses api middleware group |
| Stateful | ✅ Yes (sessions, cookies, CSRF protection) | ❌ No (stateless, uses tokens) |
| CSRF Protection | ✅ Enabled | ❌ Disabled |
| Use Case | Traditional web pages, Blade views, forms | RESTful APIs, mobile apps, SPA backends |
🖥 Web Route Example
// routes/web.php
Route::get('/dashboard', function () {
return view('dashboard');
});
URL: http://your-app.test/dashboard
📱 API Route Example
// routes/api.php
Route::get('/users', function () {
return User::all();
});
URL: http://your-app.test/api/users
🔐 Middleware Groups
webgroup → Handles session, cookies, CSRF, authenticationapigroup → Throttle requests, uses token-based authentication
You can check these in app/Http/Kernel.php.
api.php routes and Laravel resources.


