在第一篇的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()
{