1. Start the analysis from the entry file

The entry file for Laravel is /public/index.php. In index.php, the code to handle the request is:

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
Copy the code

Here, $kernel is an instance of the kernel implementation bound to the service container above, namely App\Http\ kernel ::class.

2. Analyze the handle () method called by $kernel

It calls the handle () method, which is not available in app\ http\ kernel.php, so it actually calls the handle method in its parent class.

Its parent, the handle () method, looks like this:

public function handle($request) { try { $request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request); } catch (Exception $e) { ... } catch (Throwable $e) { ... }... return $response; }Copy the code

As you can easily see, the purpose of this method is to pass in the request and return the response.

We see that this method actually receives one

Request, so this

Request, then the request is passed by the index.php caller.

Analyze the source of $request

Index.php sent it in

request:

Request: request = Illuminate\Http\ request: :capture() Capture () looks like this:

Public static function the capture () {/ / enable support for request parameters of static: : enableHttpMethodParameterOverride (); / / will Request data assigned to create a Request object instance, and returns the return static: : createFromBase (SymfonyRequest: : createFromGlobals ()); }Copy the code

4. Go back to index.php again

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
Copy the code

The $request object contains all request parameters, such as request data, request method, request URL, request header, user IP, whether to use HTTPS, etc.

Then, in index.php

The kernel (the core class that handles HTTP requests) holds this request instance

The kernel (the core class that handles HTTP requests) takes the request instance, handles (), and gets the response.