[项目名]/public/index.php
$app = require_once __DIR__.'/../bootstrap/app.php';
//获得Http内核Kernel的示例;构造HttpKernel
$kernel = $app->make(Kernel::class);
//处理请求和响应;发送响应信息
$response = tap($kernel->handle(
$request = Request::capture()
))->send();
//结束所有中间件以及可终止程序;完成
$kernel->terminate($request, $response);
[项目名]/bootstrap/app.php
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
//设置接口类的实现类,并使用单例模式实例化实现类。
//注册HttpKernel
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
//注册ConsoleKernel
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
//异常处理
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
return $app;
[项目名]/Http/Kernel.php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Illuminate\Session\Middleware\StartSession::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Language::class, //语言包中间件注册。
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}
入口页,public/index.php;引入了 bootstrap 目录下的 app.php,通过 make 方法实例出一个 kernel 对象,Kernel 类是在 Illuminate\Contracts\Http\Kernel ,打印出 Kernel 对象,发现有很多 protected 的属性,简单罗列一下:
* middleware
* middlewareGroups
* routeMiddleware
* app
* router
* bootstrappers
* middlewarePriority
重点看 app 属性,app 属性实际上是一个 Illuminate\Foundation\Application 的实例,里面又包含很多属性值,主要的属性有:
* loadedProviders
* bindings
* instances
* aliases
接着调用了 kernel 类的 handle 方法,传入一个 request 请求,request 请求继承了 SymfonyRequest,并调用了一个 capture 方法,并将返回的值返回给 response 变量。整个流程基本到此为止。
,难道就这么简单,当然不是,请求进来之后,kernel 类做了一系列的验证,分发,处理,包括表单验证,中间件,日志记录,调用控制器,查询数据等等.... 如果能把这中间的整个流程全部搞清楚,并理解为什么这么做,那么你就可以算个咖了,起码是个中咖!
$response = tap($kernel->handle(
$request = Request::capture()
))->send();
$kernel->terminate($request, $response);
/bootstrap/app.php
// 创建容器$app;参数为应用程序根目录
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
该类是框架核心类,负责启动框架,以及调动其他类提供的功能。
该类继承了Illuminate\Container\Container类,可见该类也是个容器。是整个框架最大的容器;
执行流程
1加载框架类库,创建容器,接收request请求。
2以管道的设计模式 执行部分中间件
3路由解析调度
4实例化当前控制器
5执行web中间件
6执行控制器中间件
7执行控制器操作
8返回response
9接收框架