December 13, 2023

Understanding Laravel Directory Structure

Laravel, known for its elegant syntax and developer-friendly features, takes pride in its meticulously designed directory structure. This structural foundation serves as a blueprint for promoting code organization, separation of concerns, and facilitating a seamless development experience. Let’s delve into the key components of the typical Laravel directory structure to unravel the secrets behind its efficiency.

Here is the basic Laravel directory structure visualization

├── 📂app
│   ├── 📂Console
│   ├── 📂Exceptions
│   ├── 📂Http
│   │   ├── 📂Controllers
│   │   └── 📂Middleware
│   ├── 📂Models
│   └── 📂Providers
├── 📂bootstrap
│   └── 📂cache
├── 📂config
├── 📂database
│   ├── 📂factories
│   ├── 📂migrations
│   └── 📂seeders
├── 📂public
├── 📂resources
│   ├── 📂css
│   ├── 📂js
│   └── 📂views
├── 📂routes
├── 📂storage
│   ├── 📂app
│   │   └── 📂public
│   ├── 📂framework
│   │   ├── 📂cache
│   │   │   └── 📂data
│   │   ├── 📂sessions
│   │   ├── 📂testing
│   │   └── 📂views
│   └── 📂logs
├── 📂tests
│   ├── 📂Feature
│   └── 📂Unit
├── 📂vendor
├── 📃composer.json
├── 📃composer.lock
├── 📃.env
├── 📃.env.example
├── 📃artisan
├── 📃package.json
├── 📃phpunit.xml
  • app: App directory contains application’s core code. app directory is a fundamental part of the application’s structure. It is one of the core directories where you’ll be spending most of your time writing application code. The app directory typically resides at the root of your Laravel project, and it contains various subdirectories and files that organize your application’s code. 
  • Http: The Http directory is where most of your application’s HTTP-related code resides. This includes controllers, middleware, form requests, and more.
    • Http/Controllers: Controllers define the application’s HTTP request handling logic. Controllers are responsible for handling incoming HTTP requests, processing data, and returning responses.
    • Http/Middleware: Middleware allows you to filter HTTP requests entering your application. Middleware can perform tasks like authentication, logging, and request modification.
    • Http/Requests: Form requests are used to validate incoming data from HTTP requests, making it easier to validate and sanitize user input.
  • Console: console directory is where you store and manage your custom Artisan commands. Artisan is a command-line tool included with Laravel that provides various commands to help you manage and develop your Laravel applications more efficiently. The console directory is specifically dedicated to creating and organizing custom commands that you can run via the command line.
  • Exceptions: The Exceptions directory contains custom exception classes. You can define your application-specific exception handling logic here to catch and handle different types of exceptions that might occur during application execution.
  • Models: Model is simplifies database interactions by allowing you to work with databases using object-oriented syntax. Models represent database tables and encapsulate the business logic for interacting with those tables
  • Providers: The Providers directory contains service provider classes. Service providers are responsible for binding classes into the service container, registering services, and performing other bootstrap tasks for your application..
  • bootstrap: Contains the application’s bootstrapping scripts, including the app.php file that initializes the Laravel application.
  • config: Contains configuration files for various aspects of your application, such as database connections, cache settings, and more.
  • database: database directory is where you manage various aspects of your application’s database, including database migrations, seeders, and database factories.
    • migrations: database/migrations is where you store database migration files. Migrations are a way to version control your database schema changes. Each migration file contains a set of instructions to modify the database schema, such as creating tables or adding columns. You can run migrations using the Artisan command-line tool to apply schema changes to your database.
    • seeders: database/seeders is where you define database seed classes. Seeders are used to populate your database with initial or sample data. By running seeders, you can fill your database tables with test data or set up default records.
    • factories: database/factories is where you define database factory classes. Factories provide a convenient way to generate fake or test data for your application’s models
  • public: This is the web server’s document root. It contains publicly accessible assets like CSS, JavaScript, and media files. The index.php file in this directory is the entry point for incoming HTTP requests.
    • index.php: This file is the entry point for all incoming HTTP requests to your Laravel application. It loads the Laravel framework and routes the requests to the appropriate controllers and actions based on your defined routes
    • CSS, JS, and Assets: You can store your CSS stylesheets, JavaScript files, images, fonts, and other publicly accessible assets in this directory. These assets are available for direct access via the web browser
  • resources: Contains uncompiled assets, such as Blade templates, Sass files, and JavaScript files.
    • views: resources/views is where you store your Blade templates. Blade is Laravel’s templating engine. Blade templates allow you to define the HTML structure of your application’s user interface, and they can include dynamic content using Blade’s templating syntax.
    • lang: resources/lang is where you store language files. Laravel supports localization and internationalization, and you can use language files to provide translations for your application’s text elements.
    • assets: resources/assets is often used for uncompiled assets like Sass or LESS files, JavaScript files, and other front-end assets. These files can be compiled and minified into the public directory for production use.
  • routes: routes directory contains files that help you define these routes in an organized way.
    • web.php: routes/web.php is where you define web routes for your application. These routes typically handle HTTP requests and are intended for web pages accessible through a web browser. You can define routes for various HTTP methods like GET, POST, PUT, and DELETE.
    • api.php: routes/api.php is where you define routes specifically for your API endpoints. These routes are designed for RESTful API interactions and are often stateless.
    • console.php: routes/console.php is where you define Artisan console commands and scheduled tasks. You can use this file to register custom console commands that you can run from the command line using Artisan.
  • storage: storage directory is a critical part of the application structure that is used for storing and managing various types of data generated or used by your Laravel application. This directory serves as a centralized location for storing data that should not be publicly accessible via the web, such as cached files, logs, file uploads, and application-generated files. The storage directory is usually located in the root directory of your Laravel project.
    • app: app directory’s primarily used for storing application-specific data generated during runtime. It may include files like logs, cache data, and session files.
    • framework: Contains framework-generated files like cache and session data.
    • logs: This subdirectory is dedicated to storing log files generated by the application..
    • app/public: Although not directly under the storage directory, this symlink (symbolic link) points to the public directory in your Laravel project. It’s used for storing publicly accessible files, such as user-uploaded images or documents.
  • tests: tests directory in Laravel is where you typically store your application’s automated tests. Laravel includes robust testing support out of the box, and the tests directory is where you can create and organize your test cases using PHPUnit, which is the default testing framework for Laravel.
  • vendor: It contains Composer dependencies. vendor directory is a fundamental part of the application structure. It is automatically generated and managed by Composer, which is a PHP dependency management tool used by Laravel for package management.
  • .env:  .env file is a crucial part of the application’s configuration. It stores environment-specific configuration variables as key-value pairs, allowing you to customize your application’s settings for different environments (e.g., development, testing, production) without modifying your code. 
  • .env.example: .env.example file is a template or example configuration file that serves as a reference for setting up your environment-specific configuration variables. It provides a list of configuration variables that your application may require, along with example values and descriptions. This file is meant to be copied and renamed to .env
  • artisan: artisan is a command-line tool that comes bundled with the framework. It provides a wide range of commands to help you manage and develop your Laravel applications more efficiently. You can use artisan to perform tasks like running migrations, generating code scaffolds, managing database seeding, and more.
  • composer.json: composer.json file is an essential part of your project’s configuration and is used by Composer, a PHP dependency management tool, to manage your project’s dependencies and autoload your application’s classes
  • composer.lock: composer.lock file is a crucial part of the application’s dependency management system. It works in conjunction with the composer.json file and is generated and managed by Composer, a PHP dependency management tool.
  • package.json: package.json file will contain information about the front-end libraries you want in your application.
  • phpunit.xml: phpunit.xml file will contain information about testing your application.

Embracing Laravel for Web Development:

Laravel’s directory structure serves as a guide, ensuring a well-organized and scalable codebase. Mastering these conventions empowers developers to navigate and contribute seamlessly. Embrace the Laravel framework’s directory structure and elevate your Laravel development experience.

Checkout our blog on How to Create Custom Package in Laravel for more details on creating your custom package with its own set of features.

Uncover the nuances of Laravel’s directory structure, master its organizational prowess, and embark on a journey of efficient and elegant Laravel web development.

Author

Share this post:
Facebook
Twitter
LinkedIn
WhatsApp

Discover more articles