
Understanding the Laravel Folder Structure
Laravel follows a well-organized directory structure that makes it easy to maintain and scale your applications. Understanding this structure is crucial for becoming an effective Laravel developer. In this comprehensive guide, we'll explore each directory and explain its purpose with practical examples.
Root Directory Overview
When you create a new Laravel application, you'll see a directory structure like this:
laravel-project/
├── app/
├── bootstrap/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── storage/
├── tests/
├── .env
├── .env.example
├── artisan
├── composer.json
├── composer.lock
├── package.json
├── phpunit.xml
└── server.php
Detailed Directory Breakdown
1. app/ Directory
The app/
directory is the heart of your Laravel application. It contains your application's core logic and follows the MVC (Model-View-Controller) pattern.
1.1 app/Http/
This directory handles HTTP-related logic.
app/Http/Controllers/
Contains controller classes that handle incoming requests and return responses.
app/Http/Controllers/
├── Auth/ # Authentication controllers
├── Controller.php # Base controller class
└── UserController.php # Example user controller
app/Http/Middleware/
Contains middleware classes that filter HTTP requests entering your application.
app/Http/Middleware/
├── Authenticate.php
├── RedirectIfAuthenticated.php
└── VerifyCsrfToken.php
app/Http/Kernel.php
The HTTP kernel that defines the application's middleware and routes.
1.2 app/Models/
Contains Eloquent model classes that interact with your database tables.
app/Models/
├── User.php # User model
├── Product.php # Product model
└── Post.php # Post model
1.3 app/Providers/
Contains service provider classes that bootstrap and configure Laravel services.
app/Providers/
├── AppServiceProvider.php
├── AuthServiceProvider.php
├── BroadcastServiceProvider.php
└── EventServiceProvider.php
2. bootstrap/ Directory
Contains files for bootstrapping the Laravel application.
2.1 bootstrap/app.php
The central application class that binds all of Laravel's core services into the service container.
2.2 bootstrap/cache/
Contains cached configuration files and routes for better performance.
3. config/ Directory
Contains all configuration files for your Laravel application.
config/
├── app.php # Application configuration
├── auth.php # Authentication settings
├── database.php # Database connections
├── filesystems.php # Filesystem settings
├── mail.php # Mail configuration
├── queue.php # Queue configuration
├── services.php # Third-party service settings
├── session.php # Session configuration
└── view.php # View settings
4. database/ Directory
Contains database-related files.
4.1 database/factories/
Contains model factories for creating fake data during testing.
database/factories/
└── UserFactory.php
4.2 database/migrations/
Contains migration files for database schema changes.
database/migrations/
├── 2014_10_12_000000_create_users_table.php
├── 2014_10_12_100000_create_password_resets_table.php
└── 2019_08_19_000000_create_failed_jobs_table.php
4.3 database/seeders/
Contains seeder files for populating your database with initial data.
database/seeders/
├── DatabaseSeeder.php
└── UserSeeder.php
5. public/ Directory
The web server's root directory. Contains publicly accessible files.
public/
├── index.php # Main entry point
├── favicon.ico # Website favicon
├── robots.txt # Search engine rules
├── .htaccess # Apache configuration
└── assets/
├── css/
├── js/
└── images/
6. resources/ Directory
Contains uncompiled assets like views, language files, and raw Sass/JavaScript files.
6.1 resources/views/
Contains Blade template files for your application's user interface.
resources/views/
├── layouts/ # Layout templates
│ └── app.blade.php
├── components/ # Reusable components
│ ├── header.blade.php
│ └── footer.blade.php
├── auth/ # Authentication views
├── home.blade.php
└── users/ # User-related views
└── index.blade.php
6.2 resources/lang/
Contains language files for localization.
resources/lang/
├── en/
│ ├── messages.php
│ └── validation.php
└── es/
├── messages.php
└── validation.php
6.3 resources/assets/
Contains raw asset files that will be compiled by Laravel Mix.
7. routes/ Directory
Contains all route definitions for your application.
routes/
├── web.php # Web application routes
├── api.php # API routes
├── console.php # Console artisan commands
└── channels.php # Broadcasting channels
8. storage/ Directory
Contains files generated by your application and is not publicly accessible.
8.1 storage/app/
Contains user-generated files and application data.
storage/app/
├── public/ # Publicly accessible files
└── framework/ # Framework-generated files
8.2 storage/logs/
Contains application log files.
8.3 storage/framework/
Contains framework-related files like sessions, cache, and compiled views.
9. tests/ Directory
Contains test files for your application.
tests/
├── Feature/ # Feature tests
│ └── ExampleTest.php
├── Unit/ # Unit tests
│ └── ExampleTest.php
└── TestCase.php # Base test case
Important Files at Root Level
.env File
Contains environment-specific configuration variables. Never commit this file to version control.
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:...
APP_DEBUG=true
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=array
SESSION_DRIVER=file
QUEUE_DRIVER=sync
.env.example File
A template file that shows what environment variables your application needs.
artisan File
The Laravel command-line tool used for managing your application.
composer.json File
Defines your application's dependencies and project metadata.
package.json File
Contains npm package definitions and build scripts for frontend assets.
Best Practices for Organizing Laravel Projects
1. Follow Laravel Conventions
Laravel follows established conventions. Follow them unless you have a good reason to deviate.
2. Use Proper Naming
Use meaningful names for controllers, models, and views.
3. Organize Models Properly
Keep your models in the app/Models/
directory and follow naming conventions.
4. Use Controllers for Business Logic
Keep controllers thin and focus on handling HTTP requests/responses.
5. Separate Concerns
Keep different types of logic separated (models for data, controllers for request handling, views for presentation).
6. Use Service Providers Wisely
Create custom service providers for application-specific services.
Common Customizations
Creating Custom Directories
You can create custom directories within app/
for better organization:
app/
├── Services/ # Custom service classes
├── Repositories/ # Data access layer
├── Helpers/ # Helper functions
└── Traits/ # Shared code between classes
Custom Route Organization
For larger applications, organize routes into separate files:
routes/
├── web.php
├── api.php
├── admin.php # Admin routes
├── user.php # User-related routes
└── product.php # Product-related routes
Conclusion
Understanding Laravel's folder structure is essential for building maintainable and scalable applications. By following Laravel's conventions and organizing your code properly, you'll create applications that are easier to understand, maintain, and extend.
Remember that the Laravel folder structure is designed with flexibility in mind. You can customize it to fit your project's needs while still following the established patterns.
Take the time to explore each directory and understand its purpose. This knowledge will serve you well as you build more complex Laravel applications!
Happy coding! 🚀