Tips for Developers New to Laravel Framework – Part 1

Introduction

Laravel is a powerful open-source MVC framework for PHP to build web applications. The Laravel framework (which is built with Symphony) was first released in February 2012 by Taylor Otwell, who is still maintaining it till this day. Laravel helps developers not “reinvent the wheel” by including commonly used tasks like authentication, routing, database interaction, and more.
Before you start learning any framework, it is highly recommended to absorb the framework lifecycle and know by heart when and why everything happens around the framework before writing any code.
And before you start learning about Laravel, the Model–View–Controller (MVC) is a must. Where an application is divided into three interconnected parts: the model, the view, and the controller.

MVC software architecture

Laravel Lifecycle

AutoLoader

As a first step, the request will be triggered by the user’s browser, then it will reach the webserver. The web server (Apache or Nginx) will redirect the given request to the Laravel public/index.php file which is simply a starting point for loading the rest of the framework. It loads the autoloader files which are generated by the composer.
Then it retrieves an instance of the Laravel application from bootstrap/app.php script. Laravel itself creates an instance of the application, which is the initial/first step.

Kernel

The next step will occur on the Kernel part of the application.
The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application. These two kernels serve as the central location that all requests flow through.
HTTP kernel, which is placed in app/Http/Kernel.php. It just receives a Request and returns a Response. Bootstrappers are defined by the Kernel class, which configures error handling, configure logging, detect environments and other tasks to be done before the request handled.
HTTP Kernel will define the list of middleware that is passed through before handled by the application.

Service Providers

The next step of the kernel is to load service providers as part of the bootstrapping action. Providers that are needed for the application are placed in config/app.php configuration file.
While the register method calls, all the providers will be registered. Once all providers are registered, then the boot method will be called.

Dispatch Request

Once the application has been bootstrapped and all service providers are registered and booted, the request will be handed over to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.

Router

Now request will be dispatched by the Router and it will end up with the views as shown below:
The router will direct the HTTP Request to a Controller or return a view or responses directly by omitting the controller. These routes will be placed in app/routes.php.
Controller app/controllers/ performs specific actions and sends data to a View.
View app/views/ formats the data appropriately, providing the HTTP Response.

Laravel Lifecycle

Eloquent Namespacing

The Eloquent ORM (object-relational mapper) included in Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each Eloquent model creates a wrapper around the database table associated with it. This makes every instance of the Eloquent model representation of a row on the associated table.
Eloquent provides accessor methods and properties corresponding to each table cell on the row. It also provides methods for establishing relationships with other models and enables you to access these models through that relationship.
Updating an Eloquent model instance updates the database record it is mapped to, and deleting it also deletes the record.
Eloquent also provides a lot of features for working with your database records.

Conclusion

This article was designed to help you get the required Laravel knowledge to get started developing web applications using the framework. As we saw in this tutorial, we’ve gone through:

  • Model–View–Controller (MVC) and what it is
  • A complete overview of the Laravel Lifecycle
  • Eloquent ORM namespacing

In the next Laravel article, we’ll cover the route model binding in Laravel framework, how to change and customize authentication user details, and how to fake PATCH and DELETE requests on form submission.

Written by Ahmed Salem
Edited by Mohamad Hussein