w3resource

Laravel (5.7) Directory Structure

Laravel's directory structure explains the different directories and subdirectories in a laravel application along with their functions.

  • Introduction
  • Root Directory
  • App Directory

Introduction

By default, the directory structure of Laravel is meant to meet the requirement for both large and small application development. But there is a provision to organize your application according to the way you like. Laravel don't put up any compulsion and restriction on locating your given class anywhere - providing the composer can is free to load that class automatically.

Where is the model directory?

When we started with Laravel, many developers are confused by the lack of models directory. however, the lack of such a directory is intentional. We find the word “models” ambiguous since it means many different things to many different people. Some developers refer to an application’s “model” as the totality of all of its business logic, while others refer to “models” as classes that interact with a relational database.

For this reason, we choose to place Eloquent models in the app directory by default and allow the developer to place them somewhere else if they choose.

Root Directory

This directory is one of the most important directories inside which you will find some other subdirectories. These are:

App Directory

The app directory holds the base code for your Laravel application. Most of all the classes in your application will be in this directory.

Bootstrap Directory

The bootstrap directory contains the app.php file that bootstrap the framework and configure autoloading. This directory also houses a cache directory which contains framework generated files for performance optimization such as the route and services cache files. It holds all the bootstrapping scripts used for your application.

Config Directory

The config directory, as the name implies, hold all of your application’s configuration file. This directory contains files such as auth.php, cache.php, database.php and so on. It’s easy to go through all the files in the directory and get acquainted with  what each of them.

Database Directory

The database directory holds your database migration and seeds. If you want, you may also use this directory to hold an SQLite database. The database directory holds your database files.

Public Directory

The public directory helps in starting your Laravel project from the index.php file and also holds other scripts (JavaScript and CSS) as well along with images required for your project

Resources Directory

The resources directory hold your views as well as your raw, un-compiled assets such as LESS, SASS or javascript. This directory also keeps all of your language files. The resources directory holds all the language (localization) files, templates (if any).

Routes Directory

The routes directory hold all of the route definitions for your application. By default, several route files are included with Laravel: web.php, api.php, console.php and channels.php.

The web.php file hold routes which provide session states, CSRF protection and cookie encryption. Most of your route will be defined in the web.php file.

The api.php file hold routes that the RouteServerProvider places in the api middleware group, which provide rate limiting.

The console.php a file is where you can define all of your closure based console commands. Each closure is bounded to a command instance allowing a simple approach to interacting with each command’s IO method. This file does not define HTTP routes. Its define console based entry points into your application.

The channel.php file is where you can register all of the event broadcasting channels that your application supports.

Storage Directory

The storage directory holds your session files, cache, compiled templates as well as miscellaneous files generated by the framework.

The storage/app/public directory is used to store user generated files, such as profile avatars, that should be publicly accessible. You may create the link using php artisan storage:link command.

Tests Directory

The tests directory hold your automated tests. An example PHPUnit is provided out of the box. It holds all the test cases.

Each test class should be suffixed with the word test. You may run your tests using the phpunit or php vender/bin/phpunit commands.

Vendor Directory

The vendor directory hold your composer dependencies.

App Directory

The majority of your application is keep in the app directory. By default, this directory is namespaced under App and is autoloaded by the composer.

The app directory holds a variety of additional directories such as console, Http and providers. Think of the console and Http directories as providing an API into the core of your application. The console directory hold all of your artisan commands, while the http directory holds your controller, middleware and requests.

Console Directory

The console directory holds all of the custom artisan commands for your application. this command may be generated using the make:command command. This directory also keeps your console kernel, which is where your custom artisan command is registered.

Events Directory

This directory does not exist by default, but will be created for you by the event:generate and make:event Artisan commands. Event may be used to alert other parts of your application that a given action has occurred, providing a great deal of flexibility and decoupling.

Exceptions Directory

The Exceptions directory hold your application’s exception handler and are also a good place to place any exception thrown by your application. If you would like to customize how your exceptions are logged or rendered, you should modify the handler class in this directory.

Http Directory

The Http directory holds your controllers, middleware and form requests. Almost all of the logic to handle request entering your application will be placed in this directory.

Jobs Directory

This directory does not exist by default but will be created for you. if you execute the make:job Artisan command. Jobs may be queued by your application or run synchronously within the current request lifecycle.

Listeners Directory

The directory does not exist by default, but will be created for you. If you execute the event:generate or make:listener Artisan commands. The listeners directory hold the classes that handle your events.

Mail Directory

This directory does not exist by default, but will be created for you. If you execute the make:mail

Artisan commands. The mail directory holds all of your classes that represent emails sent by your application.

Notification Directory

This directory does not exist by default but will be created for you. If you execute the make:notification Artisan command. The notification directory holds all of the transactional notification that is sent by your application. Laravel’s notification features abstract sending notifications over a variety of driver such as email, slacks, SMS stored in the database.

Policies Directory

This directory does not exist by default but will be created for you. If you execute the make:policy Artisan command. The policy directory holds the authorization policy classes for your application. policies are used to determine if a user can perform a given action against a resource.

Providers Directory

The provider directory holds all of the service providers for your application. Service providers bootstrap your application by binding services in the service container, registering events or performing any other task to prepare your application for incoming requests.

In a fresh Laravel application, this directory will already hold several providers. You are free to add your own providers to this directory as needed.

Previous: Laravel (5.7) Configuration
Next: Laravel (5.7) Homestead



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/laravel/directory-structure.php