Simple analysis of the implementation process and principle of laravel framework


This paper illustrates the implementation process and principle of laravel framework. Share with you for your reference, as follows:


$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
  $request = Illuminate\Http\Request::capture()

2. Enter app.php

$app = new Illuminate\Foundation\Application(
  $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)

The application class binds the base class to the container

The kernel class performs operations such as routing distribution and loading controller

3. Enter kernel.php

//The global routing middleware executes every time
protected $middleware = [];
//Middleware routing packets,
protected $middlewareGroups = [];
//Middleware alias, which can be used alone or assigned to a group
protected $routeMiddleware = [];
//Middleware sorting
protected $middlewarePriority = [];

The kernel class inherits the illuminate \ foundation \ http \ kernel class

4. Enter the illuminate \ foundation \ http \ kernel class

//HTTP method parameter override, that is, if there are parameters in x-http-method-override, follow the inside. If not, it is post
//Send the given request through Middleware / router.
$response = $this->sendRequestThroughRouter($request);
//Trigger event and call audience Han note: trigger observer
new Events\RequestHandled($request, $response)
return $response;

Enter sendrequestthroughrouter method

//Register the request class into the container
 $this->app->instance('request', $request);
//Delete the shared instance from the facade root instance
//Boot class protected $bootstrappers = []; The boot classes are all in this array
//Execute the final result through pipeline mode, slice programming, AOP 
 return (new Pipeline($this->app))
          //Sets the objects sent through the pipeline
          //Set the pipe array.
          //Shouldskipmiddleware judges whether middleware should be skipped, and $this - > make ('middleware. Disable ') is set to true
          //If $this - > bound ('middleware. Disable ') is set to true, you should be able to skip all middleware. What are the benefits of doing so?
          ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
          //Use the final target callback to run the pipeline, and the final result needs to be run

After entering the dispatchtorouter route distribution, the dispatch in the route class finally executes the route and implements the injection


Using reflection principle to realize dependency injection into illuminate \ container \ container class

public function make($abstract, array $parameters = []){
  return $this->resolve($abstract, $parameters);
public function build($concrete)
//In build
$reflector = new ReflectionClass($concrete);

Dependency injection knows what class it is through $parameter – > getclass()

class Demo{
  public function store(Request $req333, $abc){
class Request{}
$method = new ReflectionMethod('Demo', 'store');
foreach ($method->getParameters() as $parameter) {
  //Gets the type of the restriction class for the parameter
  $param_ type = $param->getClass(); // Gets the type prompt of the currently injected object
  $param_ value = $param->getName(); // Get parameter name
  if ($param_type) {
    //Extract the instance in the container from the type restriction name of the class
    $avgs[] = $app[$param_type->name];
$reflect->invokeArgs($app['demo'], $avgs);

X-http-method-override can override laravel’s requests, such as implementing restful requests, such as put delete and other special requests

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.