借鑒:http://www.cnblogs.com/wish123/p/4756669.html
Laravel所有請求的入口文件是:/public/index.php,代碼如下
<?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 nice to relax. | */
//自動加載文件設置 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. | */
//初始化服務容器,即框架的 IoC 容器,生成一個Laravel應用實例 $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. | */
//通過服務容器生成一個 HTTP / Console kernel類的實例 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
//kernel類實例運行handle方法,接收請求和處理結果(主要是運行middleware和URL相關的controller) $response = $kernel->handle( $request = Illuminate\Http\Request::capture() );
//返回處理結果 $response->send();
//為整個請求處理周期展示最后的任何想要提供的動作,如每次訪問后想要記錄用戶行為、給前端發消息 $kernel->terminate($request, $response);
(1)自動加載文件
自動加載文件功能是通過Composer實現的,在composer.json中設置的加載方式里提取加載路徑,然后根據Laravel對應的加載方式進行處理,主要為四種加載方式:
- PSR0加載方式—對應的文件就是autoload_namespaces.php
- PSR4加載方式—對應的文件就是autoload_psr4.php
- 其他加載類的方式—對應的文件就是autoload_classmap.php
- 加載公用方法—對應的文件就是autoload_files.php
具體的定義形式在 composer.json 里面設置路徑,如下:
"autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/", "App\\Http\\Models\\": "app/Http/Models/" } }, "autoload-dev": { "classmap": [ "tests/TestCase.php" ] },
(2)初始化服務容器
The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection.
這句話是指 Laravel 服務容器的核心是依賴注入(也是控制反轉,一種設計模式),它是把在類里面定義實例的這種高耦合模式摒棄掉,雖然工廠模式也行,不過也比較不靈活,然后以一種依賴的方式去把相關的類注冊在容器里,需要時再把類實例從容器提取出來注入到本身。
本質上是分離,將各種功能分離開,保證了松耦合,例如應用程序用到Foo類,Foo類需要Bar類,Bar類需要Bim類,那么先創建Bim類,再創建Bar類並把Bim注入,再創建Foo類,並把Bar類注入,再調用Foo方法,Foo調用Bar方法,接着做些其它工作。
Laravel這個過程粗略的有以下幾個步驟:
1、registerBaseBindings:初始化基本的容器實例。
2、registerBaseServiceProviders:注冊基本的service provider,有兩個:event、route。event service provider會設置隊列的處理工廠,route service provider定義一些路由行為,包括請求、反應、重定向這些。
3、registerCoreContainerAliases:注冊核心的容器功能類的別名,這些類包括驗證、緩存、配置、隊列、session等。
(3)生成kernel類
The HTTP kernel extends the Illuminate\Foundation\Http\Kernel
class, which defines an array of bootstrappers
that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled.
The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determine if the application is in maintenance mode, verifying the CSRF token, and more.
(4)kernel handle處理
將kernel當成黑盒來看,接收請求、處理請求。