Laravel tutorial for beginners part 3 (System MVC, Kernel dan Service Provider)

In the Laravel Tutorial for Beginners part 2, we have learned how to run Laravel in development mode, get to know artisan files and connect them with GIT as source code management, this time we will learn how Laravel work, MVC system in laravel, the kernel and Service Provider.

Table of Contents :

  • How Laravel works
  • Closing

 

How Laravel Works

Before we write program code in Laravel, it’s good for us to know how Laravel works, especially how the flow of requests that enter Laravel until our code is executed. Laravel uses the MVC (Model, View, Controller) concept. MVC is an architecture for designing program code in making applications that aims to break down the main program code into 3 separate components with specific tasks.
These three components are :

1. The function of the MODEL is to access the database ()
2. VIEW functions to display the UI design (user interface).
3. The CONTROLLER functions to regulate the logic flow of the program.

All requests that enter the Laravel application will be processed first in the public/index.php, the index.php file is located in the public folder, this index.php file contains php code to load the Laravel framework then run the program code that we made, then file index.php forwards the request to the kernel class section, in Laravel there are 2 types of kernels, that is : HTTP Kernel and Console Kernel. The HTTP Kernel is used to handle requests in the form of HTTP/web while the Console Kernel is used to handle requests in the form of console commands.
in this tutorial we will discuss the HTTP Kernel, so when a web request goes to public/index.php then the request will proceed to the HTTP kernel, the kernel.php file which handles HTTP is located in the App/Http/kernel.php folder while the kernel.php which handles the console is located in App/Console/kernel.php

How laravel work – photo by shusarvada

The kernel is actually the core of the application logic, where in the kernel, incoming requests are executed until they get a response, the kernel will send incoming requests to the service provider class, so the kernel will carry out the bootstrapping process, namely loading service providers, service providers is the provider for the service, the Service Provider is responsible for bootstrapping all components in Laravel like Database, Queue, Validation, Routing and others.

Laravel will iterate over all service providers and perform registration and bootstrapping for all service providers, there are many service providers, each service provider has its own task depending on incoming requests from the kernel. file service provider located in the App/Providers/ , In the folder there are 5 files that is :

1. AppServiceProvider.php which functions is to register application services
2. AuthServiceProvider.php functions is to carry out the authentication process
3. BroadcastServiceProvider.php functions is to carry out broadcast processes related to broadcast channels
4. EventServiceProvider.php functions is to process event listeners
5. RouteServiceProvider.php functions is  to perform the routing process

After being processed at the service provider the request will proceed to routes, in routes it will be processed, if the prefix is “api” then it will be processed to routes/api.php, if the prefix is “web” then it will be processed to routes/web.php, after processing in routes then proceed to the View to display the information requested by the user.

Closing

That’s the laravel tutorial for beginners part 3, I hope it’s useful and see you in the laravel tutorial for beginners part 4.

You may want to take a look at other laravel tutorials :