Dive into the Streamlined Directory Structure in Laravel 11

We think you’ll love the fresh skeleton you start with in a Laravel 11 app that is coming out next week! Newcomers will appreciate the minimalism, and experienced developers upgrading will not experience breaking changes. You don't have to change your Laravel 10 application structure to upgrade to Laravel 11.

If you want to follow along and experiment, you can create a Laravel 10 and Laravel 11 project side by side. We used the following commands to do so:

# Update the installer
composer global update laravel/installer -W

cd path/to/projects

# Create a Laravel 10 app
laravel new laravel-10-app -n --git --pest

# Crate a Laravel 11 app
laravel new laravel-11-app --dev -n --git --pest

On the surface, the project directory structure looks identical:

However, if you start diving into the subdirectories, the file count has dropped from a fresh Laravel 11 installation by ~ 69 files:

# Fresh Laravel v10 app
$ find . -type f -not -path "./vendor/*" | wc -l
=> 217

# Fresh Laravel v11 app (as of 01/29/2024)
$ find . -type f -not -path "./vendor/*" | wc -l
=> 148

Let's review the most significant updates and see how they compare to a Laravel 10 application so you can be ready for the changes coming to fresh Laravel 11 apps.

The app Directory

The app directory has been slimmed down tremendously, moving the nine middleware that ships with Laravel into the framework and out of the project. Typically, these middleware are not heavily customized, and Laravel 11 will provide other methods to customize built-in middleware and add your own middleware.

The app directory in a fresh Laravel 11 app

Middleware changes are done through the bootstrap/app.php file, which is, according to Taylor Otwell, a “lean routes-esque style file for configuring Laravel” that looks like the following:

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

You could add an application middleware by adding something like the following in the withMiddeware() closure:

$middleware->web(append: \App\Http\Middleware\ExampleMiddleware::class);

The Kernel.php files are no longer in the Laravel project, and these are handled through the framework bootstrap/app.php file.

You might have also noticed that the Controllers directory only includes one Controller class that doesn’t extend from anything. It’s up to you how you’d like to extend your controllers (or not), but it provides a default abstract Controller class.

The config Directory

The biggest shock for you might be the updated config directory, which has…nothing inside of it (other than the .gitkeep file). You will, however, notice that many more configuration options exist in the .env.example file.

If you want to publish any given configuration file from the framework to customize it, you can do so via the config:publish command:

# config/database.php
php artisan config:publish database

# config/logging.php
php artisan config:publish logging

# Or publish all of them
php artisan config:publish

You are free to only extend the configuration values you care about, and they will be merged with the framework’s defaults so you don’t have to keep all published configuration options in a given file.

Suppose you want to look up configuration values in the framework-shipped configuration. In that case, you can use the Artisan config:showcommand, publish the config, or look it up in the config/logging.php file within the Laravel vendor folder:

php artisan config:show logging

cat vendor/laravel/framework/config/logging.php

The database Directory

The database directory is roughly the same. However, you’ll notice that the migration filenames are prefixed in a way that does not represent a given date but keeps them in order as needed. The create_personal_access_tokens.php migration file is no longer in the project. Personal access tokens are only required if you build an API, which we will cover in the routes directory changes.

Also, the database.sqlite file will be installed by default unless you pick a different database option when creating a new Laravel project.

The routes Directory

The routes directory was also slimmed down only to include the web.php and the console.php routes files. If you want to create an API or use the broadcasting functionality, you can install them via artisan:

php artisan install:api
php artisan install:broadcasting

Those commands will bring in the required migrations, JavaScript, and configuration files. What’s nice about this is that applications that don’t need broadcasting or API routes don’t have to worry about these unnecessary files being in the project.

Laravel 11 routes directory

The test Directory

The test/ directory no longer includes the CreatesApplication trait in Laravel 11 projects. If you upgrade your Laravel 10 project, you can remove this trait, as it’s now provided as part of the base TestCase from the framework.

In a Laravel 10 project, the only thing included in the base TestCase class in Laravel 10 is the CreatesApplication trait, which bootstraps the application when creating a fresh application as part of the setup before each test. You can safely remove this trait (and its usage) once you upgrade existing apps to Laravel 11.

From Laravel 10 to Laravel 11

You don't have to change your Laravel 10 application's structure at all to upgrade to Laravel 11. This will only be the structure for brand new applications and you'll still be able to use the old way if you prefer.

Learn More

If you want to learn more about Laravel 11, check out our Laravel 11 post with all the details on this exciting new release.


The post Dive into the Streamlined Directory Structure in Laravel 11 appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

Read more

© 2024 Extly, CB - All rights reserved.