DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Laravel Project Directory Structure: MVC Architecture

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.js
Enter fullscreen mode Exit fullscreen mode

Directory 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. The Kernel.php file defines the application's command schedule.

  • Events/: Houses event classes like PostCreated.php and UserRegistered.php, which are triggered during specific actions in the application.

  • Exceptions/: This directory manages exceptions in your app. The Handler.php file is used for handling exceptions globally.

  • Http/: Contains HTTP-specific files like controllers, middleware, requests, and more.

    • Controllers/: Divided into subdirectories like API/ and Web/ for handling API requests and web views respectively.
    • Middleware/: Handles HTTP request filtering. For example, Authenticate.php checks 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, like NewCommentNotification.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 include AppServiceProvider.php and AuthServiceProvider.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. The layouts/app.blade.php is the main layout file.

  • js/ and sass/: 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. 🚀

Top comments (0)