1. 程式人生 > >新手解讀:laravel 框架原始碼分析(一)

新手解讀:laravel 框架原始碼分析(一)

眾所周知,php的框架數不勝數,近幾年,一個以優雅著稱的框架,漸漸被國內phper所知道,並且開始使用,但是larave有一個很明顯的缺點就是,他的文件內容少的可憐。
而且國內的社群也不是很活躍。所以對使用這款框架的新書造成了很大困難。
作者作為一個入門也沒多久的新手,嘗試著從自己的角度,剖析一下這部框架的原理,講述一下自己踩過的坑,同時也是監督自己的學習。

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 <[email protected]>
 */

/*
|--------------------------------------------------------------------------
| 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又向核心服務中註冊了一部分重要的門面,最後返回應用的例項。