Laravel源码入门-启动引导过程(二)bootstrap/autoload.php

上篇 Laravel源码入门-启动引导过程(一)public/index.php

Laravel 由 public/index.php 开始,第一条语句是

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

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

这个语句执行实现了注册类自动载入,让我们再来看看 bootstrap/autoload.php 的代码

<?php /* bootstrap/autoload.php */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Composer Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so we do not have to manually load any of
| our application's PHP classes. It just feels great to relax.
|
*/

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

这里的 autoload.php 又引入和执行了 vendor/autoload.php。我们思考为什么不把 autoload.php直接放入public目录中呢?或许是为了目录结构上分类的更加明确,引入来自起到启动功能的 bootstrap 目录中的文件,boostrap 的目录结构见图。其中的 bootstrap/app.php 就是Laravel所谓的点亮环节所需要的创建$app的类。bootstrap/cache/services.php 看来也 bootstrap 时用到的缓存内容,里面先期写入了大量的 ServiceProvider,是否启动时载入,还未能看到源代码出处。

<?php /* vendor/autoload.php */

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit0fc4a53da1567ed0671996be0e3e77c6::getLoader();

上面是 bootstrap/autoload.php 载入和执行的 vendor/autoload.php,执行一次,这是由 Composer 产生的。

== 总结 ==

类自动载入部分,由 public/index.php 开始,到 bootstrap/autoload.php,深入到 vendor/autoload.php,最后是 vendor/composer/autoload_real.php。用目录图再回顾一下。

下篇 Laravel源码入门-启动引导过程(三)bootstrap/app.php

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章