设为首页 加入收藏

TOP

[原创] Laravel 启动流程(一)
2019-09-01 23:48:17 】 浏览:196
Tags:原创 Laravel 启动 流程

Laravel 5.5
请求到响应的整个执行阶段归纳为 4 个:

  1. 程序启动准备阶段
    • 文件自动加载
    • 服务容器实例化
    • 基础服务提供者的注册
    • 核心类的实例化
  2. 请求实例化阶段
    • 实例化 Request 实例
  3. 请求处理阶段
    • 准备请求处理的环境
    • 将请求实例通过中间件处理 及 通过路由和控制器的分发控制
  4. 响应发送和程序终止阶段
    • 将响应内容返回给客户端
    • 记录与客户端有关的信息等

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
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇如何做一个网站? 下一篇Mac OS 自带apache 启动不了的问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目