Laravel生命周期学习一

本文详细探讨了Laravel框架的生命周期,从维护模式检测到核心http请求内核的实例化,包括加载自动加载文件、初始化容器、单利注册以及管道处理等关键步骤,揭示了Laravel如何处理请求和响应。
摘要由CSDN通过智能技术生成

生命周期

  • index.php

1.1 检测是否处于维护模式

if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
   
    require __DIR__.'/../storage/framework/maintenance.php';
}

1.2 加载composer自动加载文件

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

1.3 加载初始化容器

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

1.3.1 初始化__construct()

class Application extends Container implements ApplicationContract, CachesConfiguration, CachesRoutes, HttpKernelInterface
{
   
    public function __construct($basePath = null)
    {
   
        if ($basePath) {
   
            $this->setBasePath($basePath);
        }

        $this->registerBaseBindings();
        $this->registerBaseServiceProviders();
        $this->registerCoreContainerAliases();
    }
}

1.3.1.1 设置项目的根目录路径

    public function setBasePath($basePath)
    {
   
        $this->basePath = rtrim($basePath, '\/');

        $this->bindPathsInContainer();

        return $this;
    }

    protected function bindPathsInContainer()
    {
   
        //项目的app path 
        $this->instance('path', $this->path());
        //根目录
        $this->instance('path.base', $this->basePath());
        //多语言翻译目录
        $this->instance('path.lang', $this->langPath());
        //config 配置文件路径
        $this->instance('path.config', $this->configPath());
        //public 目录路径
        $this->instance('path.public', $this->publicPath());
        //storage 目录路径
        $this->instance('path.storage', $this->storagePath());
        //数据库迁移文件路径
        $this->instance('path.database', $this->databasePath());
        //资源文件路径
        $this->instance('path.resources', $this->resourcePath());
        //bootstrap 文件路径
        $this->instance('path.bootstrap', $this->bootstrapPath());
    }

1.3.1.2 注册基本绑定到容器

    protected function registerBaseBindings()
    {
   
        //把当前实例初始给类常量 $instance
        static::setInstance($this);
        
        //把当前实例注册到app这个字符串
        $this->instance('app', $this);
    
        //把当前实例注册到 Container
        $this->instance(Container::class, $this);
        
        //单利注册渲染前端打包渲染文件
        $this->singleton(Mix::class);
        
        //单利注册 composer 包相关的类到容器
        $this->singleton(PackageManifest::class, function () {
   
            return new PackageManifest(
                new Filesystem, $this->basePath(), $this->getCachedPackagesPath()
            );
        });
    }

1.3.1.3 注册基础服务

    protected function registerBaseServiceProviders()
    {
   
        //注册事件监听
        $this
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值