您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页laravel的indx.php,Laravel源码入门-启动引导过程(一)public/index.php

laravel的indx.php,Laravel源码入门-启动引导过程(一)public/index.php

来源:华佗小知识

开始 public/index.php

看代码 public/index.php

/**

* Laravel - A PHP Framework For Web Artisans

*

* @package Laravel

* @author Taylor Otwell

*/

/*

|--------------------------------------------------------------------------

| 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 great to relax.

|

| 实际上就是引入和执行 bootstrap/autoload.php,看看里面的代码,就知道了,最终引入和

| 执行了 vendor/autoload.php,而这个文件是 Composer 自动生成的。

|

*/

require __DIR__.'/../bootstrap/autoload.php';

/*

|--------------------------------------------------------------------------

| Turn On The Lights 开灯点亮(创建$app实例)

|--------------------------------------------------------------------------

|

| 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.

|

| 开什么灯?这个灯:bootstrap/app.php,用来照亮 La 的开发,启动应用,更用它创造的价值

| 照亮用户。看看 bootstrap/app.php 的代码,可以知道,Laravel 的核心代码库取名为

| Illuminate,它不仅仅是照亮的意思,是点亮,授以灵感和启发之意。注意,这里是

| require_once,解释了前面的疑问,每次请求,都不是重新引入和执行。这一步创建了$app实例

|

| 这一句代码和上一句的一个区别,特别在初学者眼中,上一句只是包含和执行了,这一句首先是

| require_once,更重要的是 bootstrap/app.php (去看看源代码)的最后一句是

| return $app,也就是说,包含和执行后创建了 $app。

|

*/

$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.

|

| 1. $app实例有了,还要做一些准备,系统才能跑起来。这里 $app->make() 方法意味解析(resolve)

| 而不是简单的 new 操作,之所以意味着解析(resolve),是因为 $app 是一个容器,各种组件像药水

| 一样以 Injection 的方式注入到了 $app 体内,而在体外,比如 index.php 文件就相当于在 $app

| 体外,要获得 $kernel,就可以用 $app->make(类的名字) 从各种有机融合在一起的药水中来析出来,

| 这也就是 $app 是一个容器的 IoC 控制反转 的理解方式。

| 具体参见https://laravel.com/docs/5.4/container,搜索 “make method”。

| 通过 make() 获得 $kernel 实例。$kernel 的 handle() 方法,是最直接的表现:接收 $request,

| 向用户返回 $response。注意这里的 $kernel 来自 Illuminate\协议\Http\的Kernel,Laravel

| 还有很多 Kernel 类,见附图。

|

| 2. 使用Illuminate\Http\Request::capture()方法来创建 $request,当然请求由客户端发起,

| 随后,服务器(apache或内置server)接收,生成所谓的 $request,这应该就是 capture()方法

| 的定义中所谓的“Create a new Illuminate HTTP request from server variables.”

|

| 3. 这里有理由相信,每次客户端请求,都会执行capture(),生成$request,内核处理后返回

| $response。接下来是 send()方法,向客户端发回。

|

| 4. 结束请求过程,$kernel->terminate(),该方法注释为:

| Perform any final actions for the request lifecycle.

|

| 备注:Http表示不同与Console的,可以理解为web端。

|

| 备注:::class 获取类的string值,make(Illuminate\Contracts\Http\Kernel::class)

| 相当于 make('Illuminate\Contracts\Http\Kernel::class')。

|

| 备注:这里的 $request 和 $response 是Symfony的类实例,分别是

| \Symfony\Component\HttpFoundation\Request $request

| \Symfony\Component\HttpFoundation\Response $response

|

*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(

$request = Illuminate\Http\Request::capture()

);

$response->send();

// 这句话执行不到的

$kernel->terminate($request, $response);

== 总结 ==

总结:index.php做了三件事,注册类自动载入、创建$app实例,创建$kernel,让系统跑起来。前两步做的

==

附1:Laravel中的Kernel类,这点上Laravel显得乱也不乱,不好评述。

附4:require_once 语句和 require 语句完全相同,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含。include_once 语句在脚本执行期间包含并运行指定文件。此行为和 include 语句类似,唯一区别是如果该文件中已经被包含过,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。require 和 include 几乎完全一样,除了处理失败的方式不同之外。require 在出错时产生 E_COMPILE_ERROR 级别的错误。换句话说将导致脚本中止而 include 只产生警告(E_WARNING),脚本会继续运行。具体见:http://php.net/manual/zh/function.require-once.php

end

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务