Laravel folder structure: Understanding the big picture

Updated: January 15, 2024 By: Guest Contributor Post a comment

Overview

Laravel is known for its elegant syntax and robust features, which makes it one of the most popular PHP frameworks for web application development. A fundamental aspect of working with Laravel is understanding its folder structure. This guide will dive deep into the directories and files you’ll encounter in a Laravel project, helping you get a grasp on where everything belongs and how it all fits together.

Default Folder Structure

When you create a new Laravel project, the framework sets a standard directory structure. Let’s explore this structure:

  • /app: This directory contains the core code of your application. Most of the classes will be stored here, including controllers, middleware, and models.
  • /bootstrap: This directory contains the app.php file that bootstraps the framework and configures autoloading.
  • /config: As the name suggests, this directory holds all of your application’s configuration files.
  • /database: This folder contains your database migration and seed files.
  • /public: The public directory contains the index.php file, which is the entry point for all requests entering your application. This folder typically holds assets such as images, JavaScript, and CSS as well.
  • /resources: This directory houses your views, raw assets (LESS, SASS, JavaScript), and localization files.
  • /routes: All of the route definitions for your application are found in the files within this directory.
  • /storage: The storage directory contains compiled Blade templates, file-based sessions, file caches, and other files generated by the framework.
  • /tests: This directory will hold your automated tests. Laravel already sets up PHPUnit for you to start testing your application.
  • /vendor: Any packages or libraries that you install through Composer will reside in the vendor directory.

Key Directories Explained in Detail

The App Directory

Arguably one of the most important directories in your Laravel project is the /app directory. It’s crucial to understand the purpose behind its subdirectories:

  • /Console: Contains all your Artisan commands.
  • /Exceptions: Houses your application’s exception handler and is where you can add any exception-specific logging or rendering.
  • /Http: This subdirectory contains your controllers, middleware, and form request.
  • /Providers: Service providers reside here. They are the central place to configure your application.

Understanding these directories is the first step towards mastering Laravel’s architecture. Following is a closer look at what’s typically found within them.

// Console
php artisan make:command SendEmails

// Http
php artisan make:controller UserController

// Providers
php artisan make:provider UserServiceProvider

The Config Directory

The /config directory contains an array of configuration files, each responsible for different aspects of your application’s functionality. For example, database.php configures database connections, and auth.php controls authentication settings.

// Accessing a config value
$timezone = config('app.timezone');

// Setting a runtime config value
config(['app.timezone' => 'America/New_York']);

The Database Directory

Within the /database directory, you’ll find migrations, seeds, and factories to help control and test database functionalities. Migrations allow you to version your database by creating and altering tables:

php artisan make:migration create_users_table

Seeding creates predetermined datasets that can fill your tables with sample data:

php artisan make:seeder UsersTableSeeder

The Resources Directory

The /resources directory is where your blade views and raw frontend assets reside. Organizing your application’s frontend is critical for maintainability, and Laravel helps by dividing these into manageable sections.

Aspiring developers must get accustomed to the ease with which you can manage these resources and the leverage they get via Laravel:

// A blade template
@extends('layouts.app')

@section('content')
    <h1>Welcome to Our Site!</h1>
@endsection

The Routes Directory

Your application’s routes map web requests to the appropriate logic. The /routes folder contains several route files:

  • web.php: Defines the routes that require web middleware.
  • api.php: All routes that will be prefixed by /api/ in the URL.
  • console.php: Where Closure-based console commands are defined.
  • channels.php: Registers all of your event broadcasting channels.

Routes utilize controllers to delegate requests, leading to organized and modular code.

// A simple web route
Route::get('/', function () {
    return view('welcome');
});

// A route using a controller
Route::get('user/{id}', 'UserController@show');

The Storage Directory

The /storage directory must be writable by your web server, containing logs, cache, and sessions. Laravel’s command-line tool Artisan can help manage the storage directory through various commands:

// Link the public storage disk for user access
php artisan storage:link

Linking the storage facilitates serving user-generated files from your application’s storage.

The Tests Directory

Laravel is built with testing in mind. PHPUnit comes out of the box with Laravel, and it’s ready to use:

// Running all available tests
php artisan test

// Running PHPUnit directly
./vendor/bin/phpunit

Customizing Folder Structure

Laravel provides the flexibility to tailor the application structure to suit your project’s needs. It’s common for developers to add directories like Repositories or Services within the /app folder to better organize large codebases.

Always remember that Laravel’s folder structure is initially designed as per the best practices but has enough flexibility for you to adjust it as per your project requirements.

Conclusion

The Laravel folder structure offers a standard, organized framework for developing web applications. It follows a practical design intended to provide developers with a clear path to efficiency and maintainability. As you familiarize yourself with this structure, you begin to appreciate Laravel’s convention over configuration philosophy.

Understanding Laravel’s folder structure is just the beginning. With this groundwork, you can now harness the full power of this fantastic PHP framework to build feature-rich, scalable applications.