在lavarel的入口文件index.php中包含了
$app = require_once __DIR__.'/../bootstrap/app.php';
在app.php中有
$app = new Illuminate\Foundation\Application(//Vendor/laravel/framework/src/Illuminate。
realpath(__DIR__.'/../')#为lavarel的根路径
);
上方代码即建立了一个lavarel的基本容器,它是如何建立的,我们看代码
class Application extends Container implements ApplicationContract, HttpKernelInterface
{
protected $basePath;
protected $bindings = [];#存放注册的服务
protected static $instance;#存放当前对象(Application)实例
protected static $instances#存放全局共享实例
protected $resolved = [];#注册服务调用成功时如#$this->resolved[$abstract] = true;
protected $aliases = [];#别名数组,键为类或接口路径,值为别名
public function __construct($basePath = null)
{
$this->registerBaseBindings();
$this->registerBaseServiceProviders();
$this->registerCoreContainerAliases();
if ($basePath) {
$this->setBasePath($basePath);
}
}
}
此构造函数需要一个路径参数,所以我们在实例化的时候传入了一个路径,为根路径,存入$basePath。
此构造函数的第一行中registerBaseBindings()函数展开为
static::setInstance($this);