Laravel源码分析---Application类实例化(1)

本文从Laravel框架的入口开始,逐步分析Application类的实例化过程。Application类作为管理整个框架对象的核心,负责服务容器的管理。文章详细讲解了如何设置项目路径,获取目录,并执行相关函数来处理服务绑定,包括移除抽象类别名等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在第一篇的Composer加载中,已经介绍文件是如何加载进来的了。然后下一步就是将框架所用到的容器和应用都实例化起来。

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

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

首先我们实例化的入口是从这里面开始的,然后我们主要看的东西都在app.php里面。这里面我就不像第一篇文章那样直接将全部代码复制出来了,而是一步步分析。全部代码可点击这里查看app.php

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

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

这里首先实例化一个Application类,这个应用类包含了Laravel的全部应用,其中服务容器也是在这里管理的,这个相当于管理整个框架对象的类。
这里面传进去的参数其实就是项目目录,例如我的项目是/data/project/queue,那么传进去的就是这个值。

    /**
     * Create a new Illuminate application instance.
     *
     * @param  string|null  $basePath
     * @return void
     */
    public function __construct($basePath = null)
    {
   
        if ($basePath) {
   
            $this->setBasePath($basePath);
        }

        $this->registerBaseBindings();

        $this->registerBaseServiceProviders();

        $this->registerCoreContainerAliases();
    }

构造函数中将项目路径设置,其中这里面会设置一系列Laravel文件夹中的目录。

    /**
     * Bind all of the application paths in the container.
     *
     * @return void
     */
    protected function bindPathsInContainer()
    {
   
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值