• public/index.php源码如下
<?php

header("Access-Control-Allow-Headers: Origin, X-Token, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control");
header("Content-Type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Expose-Headers: Authorization');

define('LARAVEL_START', microtime(true));

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

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

$response->send();

$kernel->terminate($request, $response);
  • 先通过header函数设置一些请求头。

  • define('LARAVEL_START', microtime(true));定义了请求开始时间。

  • require __DIR__.'/../vendor/autoload.php';加载composer的类自动加载。

  • $app = require_once __DIR__.'/../bootstrap/app.php';加载Laravel核心app。

  • booststrap/app.php源码如下

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')  // 项目根目录
);

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);


return $app;
  • bootstrap/app.php中创建了Laravel核心app, 并绑定了Http请求内核,Console内核和异常处理单例,
  • 暂且不看Illuminate\Foundation\Application, 跳回入口文件public/index.php,$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);从容器解析kernel对象,make方法先不讨论。

  • $response = $kernel->handle( $request = Illuminate\Http\Request::capture() );请求捕获处理,得到响应,$response是Symfony\Component\HttpFoundation\Response的实例化对象。

  • $response->send();发送响应。

  • $kernel->terminate($request, $response);调用所有在用中间件的终止函数,调用容器的终止函数,做 Laravel 最后的收尾工作。