An overview,
A Web server’s handling of AN HTTP request goes something like this: it listens for the request on a port, runs the program when it comes in, and then sends the results of the program as a response.
The same is true for Web applications built on the Laravel framework that handle HTTP requests. All HTTP requests are forwarded to the single-entry file /public/index.php.
Second, the analysis of
Analyze the index. PHP code line by line.
1. Introduce auto-loading
require __DIR__.'/.. /vendor/autoload.php';Copy the code
2. Create an Application instance as a global service container, and bind the Kernel implementation instance that processes requests to this container.
$app = require_once __DIR__.'/.. /bootstrap/app.php';Copy the code
Note: processing in app.php file:
<? $app = new Illuminate\Foundation\Application($_ENV['APP_BASE_PATH']?? dirname(__DIR__) ); // Bind the interface that handles HTTP requests to the service container $app->singleton(Illuminate\Contracts\ HTTP \Kernel::class, app \ HTTP \Kernel::class); // Bind the interface that handles CLI requests to the service container $app->singleton(Illuminate\Contracts\Console\Kernel::class, app \Console\Kernel::class); $app->singleton( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class ); // Return $app;Copy the code
3. Parse the Kernel instance that processes HTTP requests from the service container
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
Copy the code
The make method here, in Illuminate Foundation Application, is all about parsing
4. Core code for handling HTTP requests
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
Copy the code
$kernel is the parent of App\Http\ kernel, which Illuminate\Foundation\Http\ kernel, and we go to the handle() method
/** * Process the incoming HTTP Request, get a Request, return a Response * type the HTTP Request, * * Handle an incoming HTTP request. * @param \Illuminate\ HTTP \ request $request * @return \Illuminate\Http\Response */ public function handle($request) { try { $request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request); } catch (Exception $e) { $this->reportException($e); $response = $this->renderException($request, $e); } catch (Throwable $e) { $this->reportException($e = new FatalThrowableError($e)); $response = $this->renderException($request, $e); } $this->app['events']->dispatch( new RequestHandled($request, $response) ); return $response; }Copy the code
We find that the method that is actually being processed is sendRequestThroughRouter()
protected function sendRequestThroughRouter($request) { $this->app->instance('request', $request); Facade::clearResolvedInstance('request'); $this->bootstrap(); $this->bootstrap(); ** The router matches the request URL path with all the routes registered by the application. If there are any matching routes, All the routing middleware allocated by the route is collected first * to filter the request through these routing middleware. Only after all routing middleware passes the verification, it will run the corresponding anonymous function or controller method and execute the corresponding request processing logic. Finally, it will prepare the response to be sent to the client. */ return (new Pipeline($this->app)) ->send($request) ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) ->then($this->dispatchToRouter()); }Copy the code
5. Send the response
$response->send();
Copy the code
6. Terminate the program and do some cleaning up (mainly running terminating middleware and registering terminating callbacks to the service container)
$kernel->terminate($request, $response);
Copy the code