Laravel 5.6 Folder Structure
Since each and every framework has its unique bootstrap implementation, it becomes necessary to first have an eagle view of its structure and working methodology. And same goes with Laravel. Laravel does inherit its MVC architectural look from multiple frameworks, but its folder structure is really similar to Symphony (especially the view directory).
Looking at Laravel’s folder structure, you may feel a bit overwhelmed if it happens to be your first framework. But trust me this folder structure make your job as a developer much easier. For now, let us have a quick look at them (in Laravel 5.6):
- Root Directory
- App Directory
# The Root Directory
• The App Directory:
This directory consists of core files and code of the application. Since the majority of the application is housed in this directory, we will discuss each of its elements a little later in this post.
• The Bootstrap Directory:
This is the directory for bootstrapping scripts of the application. It is for performance optimization and thus also has cache sub-directory for framework generated files.
• The Config Directory:
As the name suggests, it contains all the configuration files of the application like app, database, mail, etc.
• The Database Directory:
It stores database migrations and seeder files in their respective folder. Laravel provides user and password migrations by default. Optionally, it can also hold an SQLite Database.
• The Public Directory:
This is the main entry point of the application. Here
index.php file is placed which receives all the incoming requests entering the application. It also configures autoloading and optionally, styles, scripts and images can be fetched from this directory.
• The Resources Directory:
This directory is for raw, uncompiled application assets like LESS, SaSS, JS and localization and language files. It can also have language and template files of the application.
• The Route Directory:
Routes directories have multiple files to store application routes in pretty URL format for web, API, console, etc
• The Storage Directory:
This contains compiled asset, blade templates, sessions, caches and log files plus app storage like uploads, etc in respective sub-folders.
- App: It stores any files generated by an application.
- App\Public: User uploaded files like avatar, etc which are publicly accessible.
- Framework: It stores framework generated caches and files.
- Logs: It generally stores log files of an application.
• The Test Directory:
Developers can store their automated tests with test cases for the application here. By default, PHPUnit is provided.
• The Vendor Directory:
This directory contains numerous supporting frameworks, composer dependencies, libraries and code to bring an application in a working state.
# The App Directory:
Console Directory can contain user-defined custom artisan commands in the application
It is a repository for application events that can be used to send alerts to other components of the application. Thus, it promotes decoupling.
This contains exception handler of the application where developers can add exception thrown by the application.
This is one of the most used directories. It stores controllers and middleware, each in their respective folders. It comes with default auth controller and some middleware.
Policies are used to validate if a user can perform a certain action against a resource. Thus, this directory contains various authorization policy classes of the application.
Service Providers of the application go under this folder. Service Providers can register events or perform specific tasks as a preparation for upcoming requests. Laravel also provides some ready-to-use default providers for authentication, broadcasting, etc.
There can also be some other directories which don’t exist in a fresh Laravel project by default. Artisan makes command can create one. Let us learn about them:
It contains queueable jobs of the application. They can either be queued or run synchronously in the current request life-cycle. Artisan command –
make:job can be used to create this sub-folder.
It contains event handler classes. Event Listeners usually receive an event request and send a response to the fired event after performing required logic. Artisan command –
make:listener can be used to create this sub-folder.
It contains mail classes that represent emails sent by the application. Mail objects help to encapsulate the logic of building emails in a simple and single class. Artisan command –
make:mail can be used to create this sub-folder.
This directory stores all the transactional notifications sent by the application. Laravel supports various drivers like email, Slack, SMS, stored database to send notifications. Artisan command –
make:notification can be used to create this sub-folder.
It holds custom validation rule objects for the application. This rules can be used to simplify and encapsulate complicated logics. Artisan command –
make:rules can be used to create this sub-folder.
Folder Structure Best Practices
Laravel has had a really long and interesting journey. It had powerful features yet shallow folder structure. But with regular updates in the framework, the picture has completely changed.
Initially, developers had to create some folders for specific task all by themselves, but as you would have noticed in the above sections, most of the tasks and aspects are taken care of in version 5.6 by default. Hence, we can say that Laravel framework itself follows Best Practices for Folder Structures, thereby reducing the efforts of developers
In this post we learned about various directories available in Laravel 5.6. We specifically discussed each folder and its utility. In the upcoming post, we will also talk about typical features of version 5.6. Stay Tuned!