Laravel Directory Structure Explained
Summary:
Understand the MVC architecture and Laravel folder structure.
Laravel is one of the most popular PHP frameworks, renowned for its elegance and simplicity. To take full advantage of Laravel's features and maintain clean, organized code, it’s essential to understand its directory structure and the Model-View-Controller (MVC) pattern on which it is built. In this post, we’ll take a guided tour through the Laravel directory structure, explaining the purpose of each folder and how it relates to MVC architecture.
Laravel’s MVC Philosophy
At its core, Laravel implements the MVC (Model-View-Controller) architectural pattern:
- Models handle data and business logic.
- Views display data to users.
- Controllers process requests and return responses.
Laravel’s directory structure enforces this separation, making apps easy to scale and maintain.
Root-Level Structure
When you create a new Laravel project (laravel new projectname
), you’ll see a directory tree like this:
project/
├── app/
├── bootstrap/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── storage/
├── tests/
├── vendor/
├── .env
├── artisan
└── ...
Let’s explore what each folder is for:
1. /app
This is where most of your application code lives. The folder follows the MVC structure:
- /app/Models
Stores your model classes. Models represent database tables and contain business logic.
- /app/Http/Controllers
Houses your controller classes. Controllers handle user requests, process data using models, and return responses (views or JSON).
- /app/Http/Middleware
Contains middleware classes used to filter HTTP requests, e.g., authentication checks.
- /app/Providers
Service providers for bootstrapping various Laravel services.
- /app/Console
, /app/Events
, /app/Jobs
, /app/Policies
, etc.
Other directories for console commands, event handling, queued jobs, and authorization logic.
2. /bootstrap
Contains files for initializing Laravel, including the app.php
file which sets up the application and the cache/
folder for framework-generated files like compiled routes/services.
3. /config
Configuration files for your application—database, cache, session, mail, and more. Each aspect of Laravel’s behavior can be fine-tuned here.
4. /database
All things database-related:
factories/
for model factories (used in seeding and testing)migrations/
for managing schema changesseeders/
for populating test data
5. /public
The web server’s document root. Serves the application to users. Contains:
index.php
(Laravel’s front controller)- Static assets (images, CSS, JavaScript)
Never put sensitive files here.
6. /resources
Everything needed to build the user interface lives here:
views/
for Blade templates (Views in MVC)lang/
for language files (localization)js/
,css/
, andsass/
for frontend assets (pre-compiled by Laravel Mix)
7. /routes
Where all the route definitions are kept. Laravel separates routes based on expected usage:
web.php
– for web (browser) routesapi.php
– for API endpointsconsole.php
andchannels.php
for artisan commands and event broadcasting channels
Route files connect URLs to controllers (MVC in action!).
8. /storage
Where Laravel stores generated files, logs, and cache:
app/
for user-generated filesframework/
for framework cache and sessionslogs/
for application logs
Make sure the correct storage subfolders are writable by your web server.
9. /tests
Contains your application’s automated tests, both feature and unit tests.
10. /vendor
Managed by Composer, this directory holds all PHP packages and framework source code your application depends on.
Other Important Files
.env
— Environment configuration (database URLs, API keys, etc.)artisan
— Laravel’s command-line tool (php artisan
)composer.json
— Lists PHP dependenciespackage.json
— Lists JavaScript dependencies
How All These Folders Work Together
Here’s how a typical MVC request moves through these directories:
- User visits a URL →
/public/index.php
- Router (
/routes/web.php
) matches the URL to a Controller (/app/Http/Controllers/
) - The Controller uses a Model (
/app/Models/
) to fetch or change data, then returns a View (/resources/views/
) - The response is sent back to the user.
Conclusion
Laravel’s directory structure is meticulously crafted to support best practices in web development. Each folder has a clear purpose, keeping your code modular, organized, and easy to maintain. By understanding the MVC architecture and the placement of files in a Laravel project, you’ll be able to build powerful, scalable PHP applications with confidence and clarity.
Further Reading:
Happy coding!