Laravel Muhammad Manamil · November 10, 2025

Laravel 12 Routing – Defining Routes in bootstrap/app.php

Laravel 12 Routing – Defining Routes in bootstrap/app.php

Introduction

Laravel 12 has simplified how applications are bootstrapped and routes are registered.
Instead of the traditional routes/web.php and routes/api.php being loaded automatically through RouteServiceProvider, Laravel 12 gives developers more control by defining routes directly in the bootstrap/app.php file.

This new approach makes the app startup process faster, cleaner, and easier to customize — especially for microservices or lightweight Laravel setups.

1. Old vs New Routing System

Before Laravel 12, your routes were usually registered through the RouteServiceProvider:

// In routes/web.php
Route::get('/', function () {
    return view('welcome');
});

Laravel automatically loaded these files via the provider.
But from Laravel 12 onward, you can define routes directly inside bootstrap/app.php using the new closure-based bootstrapping style.

2. The New bootstrap/app.php Structure

A typical bootstrap/app.php file in Laravel 12 looks like this:

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // Register your middleware
    })
    ->withExceptions(function (Exceptions $exceptions) {
        // Handle custom exceptions
    })
    ->create();

But now, you can define routes right inside this file using closures.

3. Defining Routes Directly in bootstrap/app.php

Example of defining a route inline without creating web.php:

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Application;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        using: function () {
            Route::get('/', function () {
                return 'Welcome to Laravel 12!';
            });

            Route::get('/about', function () {
                return 'This is the about page.';
            });

            Route::get('/contact', function () {
                return 'Contact us at support@example.com';
            });
        }
    )
    ->create();

This method:

  • Removes the need for extra routes/web.php or RouteServiceProvider.

  • Speeds up boot time.

  • Keeps configuration and routes in one place — perfect for small or API-only apps.

Combining Inline and External Routes

If you still want to keep traditional route files, you can mix both approaches:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        using: function () {
            Route::get('/', fn() => 'Home Page');
        },
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php'
    )
    ->create();

This gives you flexibility: define quick routes inline and load complex ones from files.

5. Why This Change in Laravel 12?

Taylor Otwell introduced this cleaner structure for several reasons:

  • Performance: Fewer service providers to boot.

  • Clarity: All core app configuration lives in one file.

  • Flexibility: Ideal for small services or when you want to customize how routes are loaded.

6. Common Mistakes to Avoid

  1. Forgetting to import Route:
    Always add use Illuminate\Support\Facades\Route; at the top.

  2. Misplacing closure syntax:
    The closure for using: must be properly enclosed.

  3. Missing trailing commas or parentheses can break your configuration chain.

  4. Mixing route groups incorrectly — wrap them properly if using middleware.

Example:

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', fn() => 'Dashboard');
});

7. Example: API Routes in bootstrap/app.php

You can also define API routes like this:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        using: function () {
            Route::prefix('api')->group(function () {
                Route::get('/users', fn() => ['user' => 'John Doe']);
                Route::post('/login', fn() => ['message' => 'Login success']);
            });
        }
    )
    ->create();

This makes it super easy to build simple APIs without needing the full Laravel directory structure.

Conclusion

Laravel 12 brings a fresh, modern approach to routing by allowing routes to be defined right in bootstrap/app.php.
This update simplifies project setup, improves flexibility, and makes Laravel more modular — especially useful for microservices and small apps.

Whether you define routes inline or in separate files, Laravel 12 ensures smooth routing management with better control.

Frequently Asked Questions (FAQs)

1. Can I still use routes/web.php in Laravel 12?

Yes, Laravel 12 still supports traditional route files. You can use both inline routes in bootstrap/app.php and external route files simultaneously.

2. Why did Laravel move routes into bootstrap/app.php?

The goal is to simplify application bootstrapping and improve performance by reducing provider overhead and centralizing configuration.

3. Is it necessary to define all routes in bootstrap/app.php?

No. It’s optional. You can keep using routes/web.php and routes/api.php if you prefer the classic structure.

4. Will this affect existing Laravel 10 or 11 projects?

No. Older versions will keep using the previous structure. This change applies only if you upgrade to Laravel 12 and adopt the new setup.

5. Can I use middleware and route groups in bootstrap/app.php?

Absolutely. Middleware, groups, and route parameters work exactly as before. Just make sure to import the Route facade properly.

Read next.

All articles
Laravel Nov 10, 2025 Laravel 12 Social & Passkey Authentication (AuthKit) – Complete Guide

Authentication is one of the most important parts of any modern web app — and Laravel 12 makes it easier than ever with AuthKit, which supports social logins (like Google, GitHub, Facebook) and passkey authentication (passwordless login using Face ID, Touch ID, or security keys). In this guide, you’ll learn step-by-step how to set up Social Login + Passkey Authentication in Laravel 12 using AuthKit — from scratch.

Read article
Laravel Nov 10, 2025 Laravel 12 – Fixing storage:link Asset Error

Facing the “storage:link” asset error in Laravel 12? This guide walks you through the exact fixes for missing symlinks, incorrect file paths, and permission issues. Learn how to reconnect your public/storage link, adjust filesystem settings, and make uploaded files publicly accessible on both VPS and shared hosting environments.

Read article