Learn about Request Lifecycle

Surely you've heard or used PHP Framework as Laravel, right? So what happens when there is a request to a URL defined in the application route? How does the Framework work? This article will help you understand more about this Framework.

166c3abbf40dd41bb1463e38e8cbc2e9683720dc

What is the standard Lifecycle?

  • An HTTP Request routes to a Controller
  • Controller implements specific actions and transfers data to View
  • View format data appropriately. Provides HTTP Response

There are many exceptions and variances with flow on, but this gives you three basic places to start:

  1. Routing in app / route.php
  2. Controller in app / controllers /
  3. View in app / views /

Some of the above exceptions are:

  • The route returns Views or Response directly, ignoring the use of Controller
  • Filters (in app / filters.php ) may appear before or after the route
  • Error and exception handling
  • Feedback Events

A deep understanding of Request Lifecycle shows some places where we can write code. The entire Lifecycle of a request can be divided into 3 parts: Loading, Booting and Running.

Part 1: Loading

screen-shot-2016-11-30-at-5-11-43-pm

There are 3 main sections where you can influence your Loading step in Request Lifecycle.

  1. Workbench: allows you to develop and debug packages next to your application
  2. Environment Detections: you should modify bootstrap / start.php and add identification for your application
  3. Paths: You can modify bootstrap / paths.php to customize your settings. For example, change the Storage folder.

Part 2: Booting

screen-shot-2016-11-30-at-5-12-22-pm

There are 10 different areas that your application can impact on Request Lifecycle.

  1. Configuration
    The configuration of your application affects both Laravel's boot and operation process.
  2. Service Providers
    Any Service Providers that you create or link to the application are loaded early in the booting process. If your service provider is not delayed, the
    register () method will be called at this time
  3. Đang đăng nhập start file
    Your 3 application startup files (# 8, # 9, # 10 below) are registered to start when the application startup event occurs.
  4. Handle middleware going down
    Middleware works similarly to Russian nested dolls. That is, the application will launch through each of the different level classes in turn according to each level. Up to down.
  5. Booting service providers
    Now the
    boot () method on any service providers without delay will be called
  6. Booting callbacks
    Any callbacks declared in
    App :: booting () will be called
  7. Booted callbacks
    At this point our application has been started. Any callbacks declared in
    App :: booted () will be called. It includes all the callbacks loaded in the 3 files in step 3 above.
  8. Your application start script is called
    That is the file
    app / start / globals.php . This file contains the initializations you want the application to always execute before processing any requests. Laravel provides reasonable defaults for Logging, Exception, Maintenance mode. You can edit this file and put any processing you need to do in it, but make sure you keep app / filters.php .
  9. app / start / {environment} .php
    If you need the initialization code to only execute in a certain environment, you can put it in this file
  10. app / routes.php
    Route your application. This is one of the most popular files that you will edit when setting up the application.
Solve problems in Scalable Social Network - Register now!
Solve problems in Scalable Social Network – Register now!

Part 3: Step Running

screen-shot-2016-11-30-at-5-13-40-pm

There are 10 different areas that your application can impact on Request Lifecycle.

  1. Maintenance Mode
    If you have a maintenance mode listener and your application is in maintenance status then that listener will be executed.
  2. App "before" filters
    If there is any before filter registered with
    App :: before () , it will be called.
  3. Route / Controller "before" filters
    If any before filters are set at the route or controller level, it will be called
  4. Hành động
    This is where a controller method or route callback is called to handle the request.
  5. Route / Controller "after" filters
    If there are any after filters placed at the route or controller level, it will be called
  6. App "after" filters
    If there is any after filter registered with
    App :: after () , it will be called.
  7. Middleware response handling
    This is where the middleware stacks are done. Any piece of the middleware will be released and modify the response before they are returned
  8. Middleware shutdown
    If you provide any middleware deployed in
    TerminableInterface , it will call the shutdown () method.
  9. Finish callbacks
    If there are any callbacks registered with
    App :: finish () , it will be called.
  10. Shutdown callbacks
    Finally, if there are any callbacks registered with
    App :: shutdown () , it will be called.

Through this article, I hope to give you more knowledge about Request Lifecycle in Laravel application.

Share the news now