laravel 框架源碼分析


laravel框架的文檔中的例子很多時候不是很明顯,所以想要真正的使用好這個框架,我們可以嘗試去閱讀它源碼中的注釋(不得不說laravel源碼的注釋還是很詳細的)。

我們先來看一下laravel 的文件目錄結構,上圖為laravel官方給出的5.3版本目錄結構,事實上laravel對目錄結構的要求是松散的,你可以按照自己的需求,自由組合文件結構,關於各個文件夾的作用大家可以自行參考官方文檔。

現在我們開始逐步剖析laravel!

首先與一般框架不同的是laravel的入口文件在public目錄中,

 

我們看到的index.php就是我們框架的入口文件了,具體內容如下:

<?php

/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylorotwell@gmail.com>
*/

/*
|--------------------------------------------------------------------------
| 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 nice to relax.
|
| Composer為我們的應用提供了非常方便的自動加載。我們可以很簡單的引用腳本,避免了
| 我們去手動加載我們的類。
|
*/

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

/*
|--------------------------------------------------------------------------
| 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.
|
| 我們需要照亮PHP開發,所以讓我們點亮這盞燈。這里自動加載了我們的框架以便使用,
| 然后我們可以加載我們的應用來為瀏覽器的請求返回響應並讓我們的用戶愉快的使用。
|
*/

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

/*
|--------------------------------------------------------------------------
| Run The Application
| 加載我們的應用
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
| 一旦我們創建了應用,我們就可以通過核心服務來處理傳入的請求,並且發送響應的
| 返回給瀏覽器。
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);
index.php文件的第一行代碼就是就是

require __DIR__.'/../bootstrap/autoload.php';
laravel在這里引入了Composer提供的依賴注入,這樣我們就無需手動加載任何類,有關Composer的內容與本文討論的內容關聯不大,有興趣的小伙伴可以自行去Composer文檔自行查看。
第二行代碼

$app = require_once __DIR__.'/../bootstrap/app.php';
laravel 引入了bootstrap/app.php文件,
這個文件是做什么的呢,我們可以來看一下app.php的內容:

<?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.
|
| 我們首先要做的是創建一個新的Laravel應用實例以便我們將所有Laravel的組件都“粘在一起”,
| 並且這個的IoC容器綁定了Laravel的各個部分。
|
*/

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

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
| 接下來,我們需要向容器綁定一些重要的門面,以便我們能在需要的時候解決他們。
| 核心服務可以通過使用web和CLI處理進來的請求來響應應用。
*/

$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);

$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);

$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);

/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;
在這里laravel創建了一個應用實例,在
Illuminate\Foundation\Application
中laravel注冊了應用的基礎綁定,接下來,laravel又向核心服務中注冊了一部分重要的門面,最后返回應用的實例。

有需要交流的小伙伴可以點擊這里加本人QQ:luke


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM