API Routing in Laravel 11: Missing routes/api.php File (Solved)
Problem Statement
When upgrading to Laravel 11 or starting a new project, developers discover that the routes/api.php
file is missing in the standard installation. This prevents API route registration for React.js/Vue.js frontends or mobile applications needing REST/JSON endpoints. Unlike previous Laravel versions, this file is no longer included by default.
Solution: Enabling API Routes
Primary Method: Artisan Command
Laravel 11 introduces a modular structure requiring explicit API route installation:
Run the Laravel installer command:
bashphp artisan install:api
This creates:
routes/api.php
route file- Basic API route example inside the file
Verify registration in
bootstrap/app.php
:phpreturn Application::configure(basePath: dirname(__DIR__)) ->withRouting( api: __DIR__ . '/../routes/api.php', // ... )
The
api:
line must exist after running the command.
Alternative Method: Manual Configuration (For Modified Projects)
If you modified bootstrap/app.php
(e.g., using starter kits like Breeze), add this inside withRouting()
:
->withRouting(
using: function () {
// Web routes (existing)
Route::middleware('web')
->group(base_path('routes/web.php'));
// Add API routes manually
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
},
)
Common Issues & Fixes
1. Routes Not Loading After Installation
Check bootstrap/app.php
contains the API path:
->withRouting(
api: __DIR__ . '/../routes/api.php',
// ...
)
2. File Permissions (Docker/Linux)
If using Laravel Sail or Docker:
# Access container as root
docker exec -u 0 -it container-name bash
# Fix permissions
chmod -R 777 /var/www/html/routes/
3. Route Caching (Production)
After creating api.php
:
php artisan route:clear
Best Practices
- Prefer Artisan Command: Use
php artisan install:api
unless you have custom routing needs - Middleware Configuration: Verify your
api
middleware group inapp/Http/Kernel.php
- Route Prefix: Default API routes use
/api
prefix (adjust inroutes/api.php
) - Testing: Verify routes with:bash
php artisan route:list
Example route test (routes/api.php
):
Route::get('/test', function () {
return response()->json(['status' => 'API connected']);
});
Access at: http://your-app.test/api/test
Key Changes in Laravel 11
- Slimmed default installation
- Optional features added via Artisan commands
- Improved performance for non-API applications
- Clearer separation between web and API routes
⚠️ Avoid manually creating
api.php
without registering it inbootstrap/app.php
- your routes won't load.
💡 Run installations through Laravel Sail if using Docker:
sail artisan install:api
instead ofphp artisan install:api
.