Signup/Sign In

Understanding Laravel Application Root Directory Structure and Files

Posted in Programming   LAST UPDATED: SEPTEMBER 24, 2021

    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:

    1. The root directory(which contains the app directory)

    2. 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.

    List of Directories:

    1. The app directory

    2. The bootstrap directory

    3. The config directory

    4. The database directory

    5. The public directory

    6. The resources directory

    7. The routes directory

    8. The storage directory

    9. The tests directory

    10. The vendor directory

    The figure below shows the subdirectories inside the root directory:

    subdirectories inside the root directory

    Let's cover all the subdirectories one by one.

    The app directory

    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

    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

    The config directory as per its name contains the Laravel application's configuration files.

    The database directory

    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 public directory

    The Laravel project's public directory has the index.php file, which is the starting point for all the requests coming to your Laravel application and configures auto app loading event . This directory also has your Laravel application important assets such as images, JavaScript, and CSS which are used in the home view of your Laravel website.

    The resources directory

    The resources directory contains all of your views as well as your raw PHP files, and all of the un-compiled assets such as LESS, SASS, or JavaScript, etc. This directory also has all of your language files which are used to translate views or applications as per user selection preferences, if you want to make a multi-lingual application.

    The routes directory

    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:

    1. 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.

    2. 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.

    3. console.php: The console.php file is where you can define all of your closure based console commands.

    4. channels.php: Use of a channels.php file is to register the Laravel app's event broadcasting channels that your Laravel application supports.

    The storage directory

    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

    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

    The vendor directory contains all of your composer dependencies for the preferred distribution or version of your Laravel application.


    Conclusion:

    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.

    You may also like:

    About the author:
    I am a Software Engineer and freelancer with 7 years of experience
    Tags:LaravelPHPLaravel Project Structure
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS