Skip to content

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:

  1. Run the Laravel installer command:

    bash
    php artisan install:api
  2. This creates:

    • routes/api.php route file
    • Basic API route example inside the file
  3. Verify registration in bootstrap/app.php:

    php
    return 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():

php
->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:

php
->withRouting(
    api: __DIR__ . '/../routes/api.php',
    // ...
)

2. File Permissions (Docker/Linux)

If using Laravel Sail or Docker:

bash
# 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:

bash
php artisan route:clear

Best Practices

  1. Prefer Artisan Command: Use php artisan install:api unless you have custom routing needs
  2. Middleware Configuration: Verify your api middleware group in app/Http/Kernel.php
  3. Route Prefix: Default API routes use /api prefix (adjust in routes/api.php)
  4. Testing: Verify routes with:
    bash
    php artisan route:list

Example route test (routes/api.php):

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 in bootstrap/app.php - your routes won't load.

💡 Run installations through Laravel Sail if using Docker: sail artisan install:api instead of php artisan install:api.