Complete Laravel Project Directory Structure: MVC Architecture
Introduction to Laravel and Its Role in MVC Architecture
Laravel is one of the most popular PHP frameworks in the world of web development. Known for its elegant syntax and developer-friendly features, Laravel aims to make common web development tasks, like authentication, routing, sessions, and caching, much easier and more intuitive. Created by Taylor Otwell, Laravel has evolved into a full-fledged MVC (Model-View-Controller) framework, making it the go-to choice for modern web applications.
What is MVC Architecture?
The MVC (Model-View-Controller) architecture is a design pattern that separates the application logic into three interconnected components:
- Model: Represents the data and business logic of the application.
- View: Handles the display and presentation of the data.
- Controller: Acts as an intermediary between the Model and the View, handling user input and interactions.
Laravel follows this pattern to ensure that the application is clean, organized, and maintainable.
In this article, we will explore the complete directory structure of a Laravel project, specifically focusing on the MVC architecture and how each part contributes to the development process. By looking at the latest Laravel 11 directory structure, you'll be able to understand how Laravel enforces separation of concerns and keeps your codebase easy to scale and maintain.
Laravel 11 Directory Structure Overview
A standard Laravel project comes with a pre-defined structure that supports an MVC workflow, organized in a way that facilitates easy management of both backend and frontend logic. Below is the comprehensive directory structure of Laravel 11, broken down with explanations of the core directories and files you will encounter in a typical Laravel application.
๐ laravel-advanced-project/
โโโ ๐ app/
โ โโโ ๐ Console/
โ โ โโโ Kernel.php
โ โโโ ๐ Events/
โ โ โโโ PostCreated.php
โ โ โโโ UserRegistered.php
โ โโโ ๐ Exceptions/
โ โ โโโ Handler.php
โ โโโ ๐ Http/
โ โ โโโ ๐ Controllers/
โ โ โ โโโ ๐ API/
โ โ โ โ โโโ PostController.php
โ โ โ โ โโโ UserController.php
โ โ โ โโโ ๐ Web/
โ โ โ โ โโโ HomeController.php
โ โ โ โ โโโ ProfileController.php
โ โ โโโ ๐ Middleware/
โ โ โ โโโ Authenticate.php
โ โ โ โโโ RedirectIfAuthenticated.php
โ โ โโโ ๐ Requests/
โ โ โ โโโ UserRequest.php
โ โ โ โโโ PostRequest.php
โ โโโ ๐ Models/
โ โ โโโ User.php
โ โ โโโ Post.php
โ โ โโโ Comment.php
โ โโโ ๐ Notifications/
โ โ โโโ NewCommentNotification.php
โ โโโ ๐ Policies/
โ โ โโโ PostPolicy.php
โ โ โโโ CommentPolicy.php
โ โโโ ๐ Providers/
โ โ โโโ AppServiceProvider.php
โ โ โโโ AuthServiceProvider.php
โ โ โโโ EventServiceProvider.php
โ โโโ ๐ Services/
โ โ โโโ UserService.php
โ โ โโโ PostService.php
โ โโโ ๐ Traits/
โ โ โโโ ApiResponse.php
โโโ ๐ bootstrap/
โ โโโ app.php
โโโ ๐ config/
โ โโโ app.php
โ โโโ auth.php
โ โโโ database.php
โโโ ๐ database/
โ โโโ ๐ factories/
โ โ โโโ UserFactory.php
โ โ โโโ PostFactory.php
โ โโโ ๐ migrations/
โ โ โโโ 2024_01_01_000000_create_users_table.php
โ โ โโโ 2024_01_01_000001_create_posts_table.php
โ โ โโโ 2024_01_01_000002_create_comments_table.php
โ โโโ ๐ seeders/
โ โ โโโ DatabaseSeeder.php
โ โ โโโ UserSeeder.php
โ โ โโโ PostSeeder.php
โโโ ๐ lang/
โ โโโ ๐ en/
โ โ โโโ auth.php
โ โ โโโ validation.php
โโโ ๐ public/
โ โโโ ๐ css/
โ โ โโโ app.css
โ โโโ ๐ js/
โ โ โโโ app.js
โ โโโ ๐ images/
โ โโโ index.php
โโโ ๐ resources/
โ โโโ ๐ views/
โ โ โโโ ๐ layouts/
โ โ โ โโโ app.blade.php
โ โ โโโ ๐ users/
โ โ โ โโโ index.blade.php
โ โ โ โโโ show.blade.php
โ โ โโโ ๐ posts/
โ โ โ โโโ index.blade.php
โ โ โ โโโ show.blade.php
โ โโโ ๐ js/
โ โ โโโ app.js
โ โโโ ๐ sass/
โ โ โโโ app.scss
โโโ ๐ routes/
โ โโโ api.php
โ โโโ web.php
โโโ ๐ storage/
โ โโโ ๐ app/
โ โ โโโ uploads/
โ โโโ ๐ logs/
โ โ โโโ laravel.log
โโโ ๐ tests/
โ โโโ ๐ Feature/
โ โ โโโ UserTest.php
โ โ โโโ PostTest.php
โ โโโ ๐ Unit/
โ โ โโโ UserServiceTest.php
โ โ โโโ PostServiceTest.php
โโโ .env
โโโ .gitignore
โโโ artisan
โโโ composer.json
โโโ package.json
โโโ phpunit.xml
โโโ README.md
โโโ webpack.mix.jsDirectory Breakdown
1. app/ โ The Application Directory
The app/ directory is the heart of the application, where most of the business logic is placed. It contains several subdirectories that help in organizing the application components:
Console/: Contains console commands. TheKernel.phpfile defines the application's command schedule.Events/: Houses event classes likePostCreated.phpandUserRegistered.php, which are triggered during specific actions in the application.Exceptions/: This directory manages exceptions in your app. TheHandler.phpfile is used for handling exceptions globally.-
Http/: Contains HTTP-specific files like controllers, middleware, requests, and more.-
Controllers/: Divided into subdirectories likeAPI/andWeb/for handling API requests and web views respectively. -
Middleware/: Handles HTTP request filtering. For example,Authenticate.phpchecks whether the user is authenticated. -
Requests/: Contains form request validation classes (e.g.,UserRequest.php,PostRequest.php).
-
Models/: Contains the application's model classes (e.g.,User.php,Post.php,Comment.php). These models are responsible for interacting with the database.Notifications/: Contains notification classes, likeNewCommentNotification.php, that notify users about various events.Policies/: Contains policy classes (e.g.,PostPolicy.php) that manage authorization logic for models.Providers/: This directory contains service providers, which are used for bootstrapping various parts of the application. Common examples includeAppServiceProvider.phpandAuthServiceProvider.php.Services/: Custom service classes (e.g.,UserService.php,PostService.php) that are responsible for encapsulating specific application logic.Traits/: Contains reusable code snippets that can be included in models, services, or other classes.
2. bootstrap/ โ Bootstrapping the Application
The bootstrap/ directory is used to configure the application, load configuration files, and set up the initial environment. The app.php file is where the Laravel application is bootstrapped.
3. config/ โ Configuration Files
The config/ directory holds various configuration files such as app.php (application settings), auth.php (authentication settings), and database.php (database configuration).
4. database/ โ Database-Related Files
This directory contains everything related to database migrations, factories, and seeders:
-
factories/: Used for creating fake data for testing. -
migrations/: Houses migration files that define database schema changes. -
seeders/: Contains seeder classes, which populate the database with initial data.
5. public/ โ Public Assets
This is the only directory exposed to the web. It contains assets like CSS, JavaScript files, and images. The index.php file is also here, which is the entry point for all requests.
6. resources/ โ Views, Assets, and Language Files
Contains views, assets, and language files:
views/: Contains Blade templates, which are used to render views. Thelayouts/app.blade.phpis the main layout file.js/andsass/: These directories store front-end assets like JavaScript and CSS.
7. routes/ โ Defining Routes
Contains all route definitions for the application. The web.php file handles web routes, while api.php defines routes for APIs.
8. storage/ โ Files, Logs, and Cache
Contains application-generated files, logs, and cache files. The uploads/ directory stores files uploaded by users, while logs/ contains Laravel's log files.
9. tests/ โ Testing
This directory holds all tests for the application:
-
Feature/: Tests for features that involve multiple components working together (e.g.,UserTest.php,PostTest.php). -
Unit/: Unit tests for smaller components (e.g.,UserServiceTest.php,PostServiceTest.php).
10. Root Files
-
.env: Contains environment-specific settings like database credentials. -
.gitignore: Specifies files to be ignored by Git. -
artisan: The Laravel command-line interface (CLI) file. -
composer.json: Defines dependencies and configurations for Composer. -
phpunit.xml: PHPUnit configuration for running tests. -
webpack.mix.js: Configuration for asset compilation using Laravel Mix.
This breakdown of the Laravel 11 directory structure provides a comprehensive look at how everything is organized, ensuring you can develop clean, scalable, and maintainable applications. From handling requests in the controllers to working with the models and database, the structure follows the principles of MVC architecture, making it easier to manage complex applications.
๐ Complete Laravel Directory Structure (All Possible Directories at Any Level and Depth)
Each directory is followed by a brief explanation of its purpose.
๐น 1. Core Laravel Application Directories (/app)
These directories contain the application's core logic.
1๏ธโฃ /app โ Main application logic (Models, Controllers, Middleware, Services, etc.).
2๏ธโฃ /app/Console โ Custom Artisan commands and CLI-related functionality.
3๏ธโฃ /app/Console/Commands โ Subdirectory for custom Artisan commands.
4๏ธโฃ /app/Events โ Event classes for Laravel's event system.
5๏ธโฃ /app/Exceptions โ Custom exception handling for the application.
6๏ธโฃ /app/Http โ Houses controllers, middleware, and request classes.
7๏ธโฃ /app/Http/Controllers โ Controller classes that handle HTTP requests.
8๏ธโฃ /app/Http/Controllers/API โ API controllers for RESTful endpoints.
9๏ธโฃ /app/Http/Controllers/Web โ Web controllers for handling browser-based requests.
๐ /app/Http/Middleware โ Middleware that filters HTTP requests.
1๏ธโฃ1๏ธโฃ /app/Http/Requests โ Custom form request validation classes.
1๏ธโฃ2๏ธโฃ /app/Models โ Eloquent ORM models interacting with the database.
1๏ธโฃ3๏ธโฃ /app/Notifications โ Notification classes for emails, SMS, etc.
1๏ธโฃ4๏ธโฃ /app/Observers โ Model observers that listen for model events.
1๏ธโฃ5๏ธโฃ /app/Policies โ Authorization policies for defining user access control.
1๏ธโฃ6๏ธโฃ /app/Providers โ Service providers for bootstrapping application services.
1๏ธโฃ7๏ธโฃ /app/Services โ Custom service classes to handle business logic.
1๏ธโฃ8๏ธโฃ /app/Traits โ PHP traits for code reusability.
1๏ธโฃ9๏ธโฃ /app/Rules โ Custom validation rules for form requests.
2๏ธโฃ0๏ธโฃ /app/Casts โ Custom attribute casting classes for Eloquent models.
๐น 2. Laravel Bootstrapping (/bootstrap)
Contains files that handle the bootstrapping process.
2๏ธโฃ1๏ธโฃ /bootstrap โ Bootstraps the Laravel framework.
2๏ธโฃ2๏ธโฃ /bootstrap/cache โ Stores cached configuration, services, and routes.
๐น 3. Laravel Configuration (/config)
Holds configuration files.
2๏ธโฃ3๏ธโฃ /config โ Contains various Laravel configuration files.
2๏ธโฃ4๏ธโฃ /config/auth.php โ Authentication settings.
2๏ธโฃ5๏ธโฃ /config/cache.php โ Caching configuration.
2๏ธโฃ6๏ธโฃ /config/database.php โ Database connection settings.
2๏ธโฃ7๏ธโฃ /config/mail.php โ Email service configuration.
2๏ธโฃ8๏ธโฃ /config/queue.php โ Queue settings.
2๏ธโฃ9๏ธโฃ /config/services.php โ External API and service integrations.
๐น 4. Laravel Database Directory (/database)
Manages migrations, factories, and seeds.
3๏ธโฃ0๏ธโฃ /database โ Contains all database-related files.
3๏ธโฃ1๏ธโฃ /database/factories โ Model factory classes for test data.
3๏ธโฃ2๏ธโฃ /database/migrations โ Migration files that manage database schema changes.
3๏ธโฃ3๏ธโฃ /database/seeders โ Seeder classes to populate database tables.
3๏ธโฃ4๏ธโฃ /database/sql โ Raw SQL dump files for database backup or restore.
๐น 5. Localization & Language Files (/lang)
3๏ธโฃ5๏ธโฃ /lang โ Stores language translation files.
3๏ธโฃ6๏ธโฃ /lang/en โ English translation files.
3๏ธโฃ7๏ธโฃ /lang/fr โ French translation files.
3๏ธโฃ8๏ธโฃ /lang/es โ Spanish translation files.
๐น 6. Public Assets & Entry Point (/public)
3๏ธโฃ9๏ธโฃ /public โ Publicly accessible files like images, CSS, JS, and the entry index.php.
4๏ธโฃ0๏ธโฃ /public/css โ CSS stylesheets.
4๏ธโฃ1๏ธโฃ /public/js โ JavaScript files.
4๏ธโฃ2๏ธโฃ /public/images โ Static images.
4๏ธโฃ3๏ธโฃ /public/fonts โ Font files.
4๏ธโฃ4๏ธโฃ /public/storage โ Public storage linked to /storage/app/public.
๐น 7. Frontend & Views (/resources)
4๏ธโฃ5๏ธโฃ /resources โ Contains Blade views, JS, and CSS.
4๏ธโฃ6๏ธโฃ /resources/views โ Blade template files for frontend rendering.
4๏ธโฃ7๏ธโฃ /resources/css โ CSS files.
4๏ธโฃ8๏ธโฃ /resources/js โ JavaScript files for frontend behavior.
4๏ธโฃ9๏ธโฃ /resources/sass โ SASS/SCSS files for styling.
๐น 8. Routing (/routes)
5๏ธโฃ0๏ธโฃ /routes โ Defines routes for web, API, and console.
5๏ธโฃ1๏ธโฃ /routes/web.php โ Web routes (frontend).
5๏ธโฃ2๏ธโฃ /routes/api.php โ API routes (RESTful).
5๏ธโฃ3๏ธโฃ /routes/console.php โ Custom Artisan commands.
5๏ธโฃ4๏ธโฃ /routes/channels.php โ Routes for event broadcasting.
๐น 9. Laravel Storage (/storage)
Stores logs, caches, and user uploads.
5๏ธโฃ5๏ธโฃ /storage โ Stores logs, cache, and uploaded files.
5๏ธโฃ6๏ธโฃ /storage/app โ Application-specific files (backups, uploads).
5๏ธโฃ7๏ธโฃ /storage/app/public โ Publicly accessible storage files.
5๏ธโฃ8๏ธโฃ /storage/framework โ Contains framework-generated files.
5๏ธโฃ9๏ธโฃ /storage/framework/cache โ Caching data.
6๏ธโฃ0๏ธโฃ /storage/framework/sessions โ Stores user session data.
6๏ธโฃ1๏ธโฃ /storage/framework/views โ Compiled Blade templates for fast rendering.
6๏ธโฃ2๏ธโฃ /storage/logs โ Log files for debugging.
๐น 10. Testing (/tests)
6๏ธโฃ3๏ธโฃ /tests โ Unit and feature tests for the application.
6๏ธโฃ4๏ธโฃ /tests/Feature โ Feature tests for app functionalities.
6๏ธโฃ5๏ธโฃ /tests/Unit โ Unit tests for isolated components.
๐น 11. Composer & Node Dependencies
6๏ธโฃ6๏ธโฃ /vendor โ Contains all Composer dependencies.
๐ Conclusion
This is the most exhaustive list of all possible directories in a Laravel project. Some directories are always present, while others appear based on optional features, third-party packages, or specific configurations.
Understanding these directories helps you master Laravel and makes development more structured and maintainable. ๐