- 1. 程序启动准备
- 2. 请求实例化
- 3. 请求处理
- 3.1 请求处理环境初始化
- 1. 环境监测 \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class
- 2. 配置加载 \Illuminate\Foundation\Bootstrap\LoadConfiguration::class
- 3. 常处理 \Illuminate\Foundation\Bootstrap\HandleExceptions::class
- 4. 外观注册 \Illuminate\Foundation\Bootstrap\RegisterFacades::class
- 5. 服务提供者注册 \Illuminate\Foundation\Bootstrap\RegisterProviders::class
- 6. 启动提供者 \Illuminate\Foundation\Bootstrap\BootProviders::class
- 3.2 路由处理请求
- 3.3 处理返回的 Response
- 3.1 请求处理环境初始化
- 4. 响应发送和程序终止
Laravel 5.5
请求到响应的整个执行阶段归纳为 4 个:
- 程序启动准备阶段
- 文件自动加载
- 服务容器实例化
- 基础服务提供者的注册
- 核心类的实例化
- 请求实例化阶段
- 实例化 Request 实例
- 请求处理阶段
- 准备请求处理的环境
- 将请求实例通过中间件处理 及 通过路由和控制器的分发控制
- 响应发送和程序终止阶段
- 将响应内容返回给客户端
- 记录与客户端有关的信息等
1. 程序启动准备
程序入口在 index.php
中
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);
创建服务容器实例
服务容器的创建在 bootstrap\app.php
中进行.
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
1.1 容器基础配置
容器 Application
的构造函数:
public function __construct($basePath = null)
{
if ($basePath) {
$this->setBasePath($basePath);
}
$this->registerBaseBindings();
$this->registerBaseServiceProviders();
$this->registerCoreContainerAliases();
}
构造函数 主要完成以下基本配置:
目录路径(绑定到容器中, 并提供类方法获取子目录)
public function setBasePath($basePath) { $this->basePath = rtrim($basePath, '\/'); $this->bindPathsInContainer(); return $this; } protected function bindPathsInContainer() { $this->instance('path', $this->path()); $this->instance('path.base', $this->basePath()); $this->instance('path.lang', $this->langPath()); $this->instance('path.config', $this->configPath()); $this->instance('path.public', $this->publicPath()); $this->instance('path.storage', $this->storagePath()); $this->instance('path.database', $this->databasePath()); $this->instance('path.resources', $this->resourcePath()); $this->instance('path.bootstrap', $this->bootstrapPath()); }
绑定容器自身
protected function registerBaseBindings() { static::setInstance($this); $this->instance('app', $this); $this->instance(Container::class, $this); $this->instance(PackageManifest::class, new PackageManifest( new Filesystem, $this->basePath(), $this->getCachedPackagesPath() )); }
基础服务注册( Event, Log, Route)
protected function registerBaseServiceProviders() { $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); }
别名注册
多个接口名 对应一个简短别名, 后续在注册服务时只需绑定到别名上即可 (而不必绑定到具体接口名)
public function registerCoreContainerAliases() { foreach ([ 'app' => [\Illuminate\Foundation\Application::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class, \Psr\Container\ContainerInterface::class], 'auth' => [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class], 'auth.driver' => [\Illuminate\Contracts\Auth\Guard::class], 'blade.compiler' => [\Illuminate\View\Compilers\BladeCompiler::class], 'cache' => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class], 'cache.store' => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class], 'config' => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class], 'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class], 'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class], 'db' => [\Illuminate\Database\DatabaseManager::class], 'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class], 'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class], 'files' => [\Illuminate\Filesystem\Filesystem::class], 'filesystem' => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class], 'filesystem.disk' => [\Illuminate\Contracts\Filesystem\Filesystem::class], 'filesystem.cloud' => [\Illuminate\Contracts\Filesystem\Cloud::class], 'hash' => [\Illuminate\Contracts\Hashing\Hasher::class], 'translator' => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class], 'log' => [\Illuminate\Log\Writer::class, \Illuminate\