目錄
- 前言
- 入口文件
- bootstrap\app.php
- App\Http\Kernel
- 回到起點(diǎn)
本文實(shí)例講述了Laravel框架源碼解析之入口文件原理。分享給大家供大家參考,具體如下:
前言
提升能力的方法并非使用更多工具,而是解刨自己所使用的工具。今天我們從Laravel啟動(dòng)的第一步開始講起。
入口文件
laravel是單入口框架,所有請(qǐng)求必將經(jīng)過index.php
define('LARAVEL_START', microtime(true)); // 獲取啟動(dòng)時(shí)間
使用composer是現(xiàn)代PHP的標(biāo)志
require __DIR__.'/../vendor/autoload.php'; // 加載composer -> autoload.php
加載啟動(dòng)文件
$app = require_once __DIR__.'/../bootstrap/app.php';
獲取$app
是laravel啟動(dòng)的關(guān)鍵,也可以說$app是用于啟動(dòng)laravel內(nèi)核的鑰匙🔑。隨后就是加載內(nèi)核,載入服務(wù)提供者、門面所映射的實(shí)體類,中間件,最后到接收http請(qǐng)求并返回結(jié)果。
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); // 加載核心類
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
看似短短的4行代碼,這則是laravel的優(yōu)雅之處。我們開始深層次解刨。
bootstrap\app.php
這個(gè)啟動(dòng)文件也可以看作是一個(gè)服務(wù)提供者,不過他并沒有boot,register方法。因?yàn)槿肟谖募苯蛹虞d他,所有這些沒必要的方法就不存在了。
作為啟動(dòng)文件,首頁(yè)則是加載框架所有必須的要要件,例如
- registerBaseBindings
- registerBaseServiceProviders
- registerCoreContainerAliases,
這其中包括了很多基礎(chǔ)性的方法和類,例如
- db
[\Illuminate\Database\DatabaseManager::class]
- auth
[\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class]
- log
[\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class]
- queue
[\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class]
- redis
[\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class]
- 等等
...
而$app這個(gè)在服務(wù)提供者的核心變量則就是Application實(shí)例化所得,而你在服務(wù)提供者內(nèi)使用的make,bind,singleton來自他的父類Container,都說容器是laravel的核心概念。這塊的概念后續(xù)我們會(huì)詳細(xì)的講解。
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
上面我們已經(jīng)獲得$app的實(shí)例化了,現(xiàn)在通過$app來注冊(cè)核心類、異常類,并將$app返回給index.php
$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
);
App\Http\Kernel
核心類了所有的
當(dāng)然你需要使用中間件也是在這個(gè)類中加載,是經(jīng)常被使用的一個(gè)文件。
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
這個(gè)核心類繼承自他的父類Illuminate\Foundation\Http\Kernel::class
,核心類做了很多事情,它會(huì)將所有的中間件全部存儲(chǔ)到一個(gè)指定的數(shù)組,方便內(nèi)核調(diào)用及其他類調(diào)用。
namespace App\Http;
use App\Api\Middleware\VerifyApiToken;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
回到起點(diǎn)
Laravel的啟動(dòng)經(jīng)歷了很繁瑣的一個(gè)過程。這也是Laravel優(yōu)雅的關(guān)鍵點(diǎn)。
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
將請(qǐng)求傳入則完成了整個(gè)laravel的啟動(dòng),至于結(jié)果的返回則有開發(fā)者自行通過控制器或其他可訪問類返回。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Laravel框架源碼解析之模型Model原理與用法解析
- Laravel框架源碼解析之反射的使用詳解
- Laravel 框架控制器 Controller原理與用法實(shí)例分析
- Laravel框架數(shù)據(jù)庫(kù)CURD操作、連貫操作總結(jié)
- PHP開發(fā)框架Laravel數(shù)據(jù)庫(kù)操作方法總結(jié)
- Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類的方法
- Laravel框架路由配置總結(jié)、設(shè)置技巧大全
- Laravel 5 框架入門(一)
- Laravel 5框架學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)遷移(Migrations)
- Laravel 5框架學(xué)習(xí)之向視圖傳送數(shù)據(jù)
- Laravel 5框架學(xué)習(xí)之用戶認(rèn)證
- Laravel框架集合用法實(shí)例淺析