Laravel file-directory structure


The directory structure of a Laravel project is well-organized and follows a standard pattern to help developers manage their code efficiently. Here’s an overview of the key directories and files within a typical Laravel project:

1. app/

This directory contains the core logic of the application, including Models, Controllers, and Middleware.

  • Console/: Contains custom Artisan commands. You can define your own commands here.

  • Exceptions/: Handles application exceptions and contains the exception handling logic (e.g., Handler.php).

  • Http/: Contains the controllers, middleware, and form requests.

    • Controllers/: Houses all your application’s controllers (e.g., HomeController.php).
    • Middleware/: Contains the middleware used to filter incoming requests (e.g., VerifyCsrfToken.php).
    • Requests/: Custom request validation logic resides here.
  • Models/: This directory may not exist by default in recent Laravel versions, but it's commonly created to store Eloquent models representing the database tables.


2. bootstrap/

This folder contains files related to the bootstrapping process of the framework.

  • app.php: This file bootstraps the framework and loads the service providers.
  • cache/: Contains cached files for performance optimization, including route and config caches.

3. config/

The config/ directory contains all configuration files for your application. These files allow you to configure services such as the database, mail, queue, session, and more.

  • Examples: app.php, database.php, mail.php, etc.

4. database/

This directory holds database-related files like migrations, seeders, and factories.

  • factories/: Contains model factories used for generating fake data for testing.
  • migrations/: Holds migration files for creating, updating, or deleting database tables.
  • seeders/: Contains seed classes to insert dummy data into the database.

5. public/

This directory is the web server’s document root, where all publicly accessible files are stored.

  • index.php: The entry point for all requests. It handles the routing of all incoming requests to the framework.
  • css/, js/, images/: These directories typically hold your application's public assets (CSS, JavaScript, images).

6. resources/

This directory contains view files, raw assets, and localization files.

  • views/: Contains Blade templates (e.g., welcome.blade.php), which are used to render HTML pages.
  • lang/: Contains language files for localization support. Laravel ships with support for multiple languages.
  • sass/, js/: These directories hold raw frontend assets (SASS, JavaScript) which can be compiled using Laravel Mix.

7. routes/

This folder contains all the route definition files.

  • web.php: Defines routes for web-based interfaces, usually returning views (uses sessions and cookies).
  • api.php: Defines routes for APIs, typically returning JSON responses (stateless, no sessions).
  • console.php: Defines custom Artisan commands.
  • channels.php: Defines event broadcasting channels for real-time events using WebSockets.

8. storage/

This directory holds application logs, compiled Blade views, cached files, and file uploads.

  • app/: Store files that your application generates (e.g., user-uploaded files).
  • framework/: Holds framework-generated files like cache, sessions, and compiled views.
  • logs/: Stores the log files for the application. By default, Laravel logs errors and other important application information here.

9. tests/

The tests/ directory holds all test files for the application.

  • Feature/: Contains feature tests that test routes and controllers.
  • Unit/: Contains unit tests that test individual classes or methods.

Laravel uses PHPUnit for testing, and the default configuration file is phpunit.xml.


10. vendor/

This directory is where Composer stores all the third-party packages that your application depends on. This directory is not meant to be modified by hand, as Composer manages it.


11. Key Files in the Root Directory

  • .env: Environment configuration file where you define sensitive information such as the database credentials, app environment (local, production), API keys, etc.

  • artisan: The command-line interface for Laravel. It provides various Artisan commands to automate common tasks (e.g., php artisan migrate).

  • composer.json: Contains the metadata for your project’s Composer dependencies, such as package names and versions.

  • package.json: Contains the metadata for JavaScript dependencies (if you’re using tools like Laravel Mix).

  • webpack.mix.js: Configuration file for Laravel Mix, which is used to compile and bundle front-end assets like CSS and JavaScript.


Summary of Laravel Directory Structure:

project_root/ ├── app/ │ ├── Console/ │ ├── Exceptions/ │ ├── Http/ │ ├── Models/ │ ├── Providers/ ├── bootstrap/ ├── config/ ├── database/ ├── public/ ├── resources/ ├── routes/ ├── storage/ ├── tests/ ├── vendor/ ├── .env ├── artisan ├── composer.json └── webpack.mix.js

Conclusion:

Laravel’s directory structure is designed to promote good software architecture practices by separating concerns, such as routing, logic, views, and configuration. This organization makes the application easier to scale, maintain, and understand.