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

 

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