Laravel Directory Structure

Laravel Directory Structure

A comprehensive guide to understanding Laravel's directory structure and the purpose of each folder.

Introduction

Laravel follows a well-organized directory structure that makes it easy to locate and manage different components of your application. This guide will help you understand the purpose of each directory and how to use them effectively.

Prerequisites

  • Basic understanding of Laravel
  • Familiarity with MVC pattern
  • Knowledge of PHP

1 Root Directory Structure

Here's the main directory structure of a Laravel application:

laravel/
├── app/
├── bootstrap/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── storage/
├── tests/
├── vendor/
├── .env
├── .env.example
├── .gitignore
├── artisan
├── composer.json
├── package.json
└── README.md

2 Key Directories Explained

app/

The core of your application containing:

  • Models
  • Controllers
  • Middleware
  • Services
  • Providers

bootstrap/

Contains files that bootstrap the framework:

  • app.php - Application bootstrap file
  • cache/ - Framework cache files

config/

All configuration files for your application:

  • app.php - Application configuration
  • database.php - Database configuration
  • mail.php - Mail configuration
  • services.php - Third-party services configuration

database/

Database-related files:

  • migrations/ - Database migrations
  • seeds/ - Database seeders
  • factories/ - Model factories

public/

The public-facing directory:

  • index.php - Application entry point
  • css/ - Compiled CSS files
  • js/ - Compiled JavaScript files
  • images/ - Image assets

resources/

Raw, uncompiled assets:

  • views/ - Blade templates
  • lang/ - Language files
  • js/ - JavaScript source files
  • sass/ - SASS source files

routes/

All route definitions:

  • web.php - Web routes
  • api.php - API routes
  • console.php - Console commands

storage/

Generated files:

  • app/ - Application storage
  • framework/ - Framework storage
  • logs/ - Application logs

tests/

Test files:

  • Feature/ - Feature tests
  • Unit/ - Unit tests
  • TestCase.php - Base test case

3 Important Files

.env

Environment configuration file containing:

  • Database credentials
  • Mail settings
  • Application key
  • Debug mode

artisan

Command-line interface for Laravel:

  • Run migrations
  • Clear cache
  • Generate files
  • Run tests

composer.json

PHP dependencies and autoloading configuration.

package.json

JavaScript dependencies and build scripts.

Key Features Covered

  • Root directory structure
  • Purpose of each directory
  • Important configuration files
  • Asset organization
  • Testing structure
  • Storage organization

Best Practices

  • Keep controllers thin
  • Use service layer for business logic
  • Organize views by feature
  • Use proper namespacing
  • Follow PSR-4 autoloading

Common Issues

  • Incorrect file permissions
  • Storage directory not writable
  • Missing .env file
  • Incorrect autoloading
  • Cache issues