Laravel based app directory structure

To start working with Laravel we first have to understand what a basic Laravel based app consists of. Analysis and understanding of all the files and folders provided for the base of app is crucial for successful development. It eases the whole process a lot and makes it more enjoyable as well. Below we are going to explore the most important elements of directory structure:


Here is the Root Directory:

.env file
This file allows us to configurate all the environmental aspects of the application. In .env file we can among others:
- Set stage of development (local or production)
- Set MySql connection for our database
- Choose app's environment such as server type and port
- Set up mailing connection and options
- Set up connection with AWS server

 
Storage Directory
this folder consists of 3 other folders which are: app, framework and logs.


- app directory is used to store any files generated by the application. It also contains a public folder which is used to store user-generated files, that should be publicly accessible
- framework directory stores framework generated files and caches
- logs directory contains the application's log files

 Database Directory
In this folder we can find all of the database migrations, model factories and seeds. We can also store SQLite database in it.


- factories directory stores model factories which
provide a way to define data that is predictable and easy to replicate
- migrations folder stores all database migrations are version controls which allow to modify the database schema
- seeders directory contains seeds which allow including data to the database automatically

Public Directory
It consists of index.php file that
is the entry point for all requests entering the application and also configures autoloading. In this directory we can store our assets such as images, CSS and JS as well.

Resources Directory
It stores all of our views and uncomplied assets such as raw CSS and JS. Views provide a convenient way to place all of our HTML in separate files, that way we can separate our application logic from the presentation logic. 


Routes Directory
This
directory contains all of the route definitions for the application.


 For us the most important of them is web.php file which contains routes that the RouteServiceProvider places in the web middleware group, which provides session state, cookie encryption and CSRF protection. Briely saying, for setting all routes and all requests for normal web applications we use web.php. If we created a mobile app, we would then define your routes in api.php.

App Directory
This directory contains the
core code of the application and it is responsible for storing most of application's logic.


- Console directory contains all of the custom Artisan commands for the application
- In the exceptions directory we can place any exceptions thrown by the application as it strores our application's exception handler
- Almost all of the logic to handle requests entering our application will be placed in Http directory. Controllers folder stores controllers which group associated requests handling logic within single class. Middleware directory includes middleware which is a mechanism for inspecting and filtering http requests entering the application. It can, among others, help with user verification and authorisation.
- Models directory stores all models that correspond and interact with database tables. Models allow us to query for data in tables, as well as insert new records into the table.
- Providers directory stores all of te service providers for the application. Service providers binds services in the service container, registering events, or performing any other tasks to prepare the application for incoming requests.

Now when we know what which part of the structure is responsible for, we can start building the application, analyse, review and describe created code.

Komentarze