本文实例讲述了laravel框架源码解析之入口文件原理。分享给大家供大家参考,具体如下:
前言
提升能力的方法并非使用更多工具,而是解刨自己所使用的工具。今天我们从laravel启动的第一步开始讲起。
入口文件
laravel是单入口框架,所有请求必将经过index.php
define('laravel_start', microtime(true)); // 获取启动时间
使用composer是现代php的标志
require __dir__.'/../vendor/autoload.php'; // 加载composer -> autoload.php
加载启动文件
$app = require_once __dir__.'/../bootstrap/app.php';
获取$app
是laravel启动的关键,也可以说$app是用于启动laravel内核的钥匙。随后就是加载内核,载入服务提供者、门面所映射的实体类,中间件,最后到接收http请求并返回结果。
$kernel = $app->make(illuminate\contracts\http\kernel::class); // 加载核心类 $response = $kernel->handle( $request = illuminate\http\request::capture() ); $response->send(); $kernel->terminate($request, $response);
看似短短的4行代码,这则是laravel的优雅之处。我们开始深层次解刨。
bootstrap\app.php
这个启动文件也可以看作是一个服务提供者,不过他并没有boot,register方法。因为入口文件直接加载他,所有这些没必要的方法就不存在了。
作为启动文件,首页则是加载框架所有必须的要要件,例如
- registerbasebindings
- registerbaseserviceproviders
- registercorecontaineraliases,
这其中包括了很多基础性的方法和类,例如
- 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这个在服务提供者的核心变量则就是application实例化所得,而你在服务提供者内使用的make,bind,singleton来自他的父类container,都说容器是laravel的核心概念。这块的概念后续我们会详细的讲解。
$app = new illuminate\foundation\application( realpath(__dir__.'/../') );
上面我们已经获得$app的实例化了,现在通过$app来注册核心类、异常类,并将$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
核心类了所有的
- 系统中间件
- 群组中间件
- 路由中间件
当然你需要使用中间件也是在这个类中加载,是经常被使用的一个文件。
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', ], ];
这个核心类继承自他的父类illuminate\foundation\http\kernel::class
,核心类做了很多事情,它会将所有的中间件全部存储到一个指定的数组,方便内核调用及其他类调用。
namespace app\http; use app\api\middleware\verifyapitoken; use illuminate\foundation\http\kernel as httpkernel; class kernel extends httpkernel
回到起点
laravel的启动经历了很繁琐的一个过程。这也是laravel优雅的关键点。
$response = $kernel->handle( $request = illuminate\http\request::capture() ); $response->send(); $kernel->terminate($request, $response);
将请求传入则完成了整个laravel的启动,至于结果的返回则有开发者自行通过控制器或其他可访问类返回。