In this article, we will introduce you to all the subdirectories and files present inside the root directory of a Laravel Project and will cover the app directory(which is a very important directory) in the next article.
Laravel application directory structure is divided into two main directories:
The root directory(which contains the app directory)
The app directory(contains the core code)
The basic Laravel application structure is built to provide a simple starting point for both large and small applications. All of you are really free to organize your application in your own style as you like. Laravel adds almost no restrictions on where any given class is located as long as Composer can autoload the class.
The app directory
The bootstrap directory
The config directory
The database directory
The public directory
The resources directory
The routes directory
The storage directory
The tests directory
The vendor directory
The figure below shows the subdirectories inside the root directory:
Let's cover all the subdirectories one by one.
The app directory in any Laravel project directory contains the core code of your application. Almost all of the important classes in your application will be stored in this directory. By default in the Laravel app core, this directory is namespaced under App and it is autoloaded by Composer using the PSR-4 autoloading standard.
The app directory has a variety of additional directories such as console, http, and providers. Basic use of the console and the http directories are for providing an API into the core of your Laravel application. Further explanation of all the directories inside the app directory will be covered in the next article.
The bootstrap directory in the Laravel project contains the app.php file which bootstraps the framework. Also, it has a cache directory that contains the Laravel core framework generated files which may be used for performance optimization such as the route cache file and services cache files.
The config directory as per its name contains the Laravel application's configuration files.
The database directory contains your Laravel application's database migrations files, model factories files, and seeds for your Laravel application. If you want to use this directory it will provide a relational database management system - SQLite database, to store user inputs and to write the SQL queries to specify interaction between the SQLite database and the Laravel application views.
The Laravel application's routes directory contains all of the route definitions for your application. By default, several route files are included in a Laravel project like web.php, api.php, console.php, and channels.php, etc. But you can also specify your own custom routes for the application interaction and page links references. Let's see what are these default route files for:
web.php: The web.php file consists of the routes that are the RouteServiceProvider places in the web middleware group, which provides session state, CSRF protection, and cookie encryption etc. If your application does not offer a stateless, RESTful API, all of your Laravel app routes will most likely be defined in the web.php file.
api.php: The api.php file contains routes that the RouteServiceProvider places in the API middleware group, which provides rate limiting services to the container apps. The various routes from api.php routes are usually kept to be stateless routes to avoid tracking, so requests entering the application through these routes are intended to be secured and verified using tokens and they do not have access to the session state.
console.php: The console.php file is where you can define all of your closure based console commands.
channels.php: Use of a channels.php file is to register the Laravel app's event broadcasting channels that your Laravel application supports.
In the storage directory, the Laravel app core compiles the blade templates files, the Laravel app file based sessions, the Laravel app file caches, and other files generated by the Laravel app framework. Usually, in every Laravel project, the storage directory has two directories inside it - the framework directory and the logs directory. The Laravel app directory can be used to store all of the files generated by your application.
Inside the storage directory, the framework directory is used to store the Laravel app core framework generated files and app core caches. The logs directory contains your Laravel application's log files. The public directory present in storage/app/ may be used to store user-generated files, such as profile avatars, that should be publicly accessible. The Laravel app developer should create symbolic links inside the public/storage which points to this storage directory. For your Laravel app if you want to create the link, then you should use the
php artisan storage:link command.
The tests directory contains all of your Laravel application's automated tests. As an example, a PHPUnit test is provided out of the box. The Laravel app test class should be suffixed with the word Test, for example, path_Test. You may run your Laravel app tests using the
phpunit or the
php vendor/bin/phpunit command.
The vendor directory contains all of your composer dependencies for the preferred distribution or version of your Laravel application.
I hope this article helps you to understand the various components of Laravel application's root directory structure. If you face any issue, do comment and let us know.
SHARE YOUR THOUGHTS WITH US!