新手解读:laravel 框架源码分析(一)

众所周知,php的框架数不胜数,近几年,一个以优雅著称的框架,渐渐被国内phper所知道,并且开始使用,但是larave有一个很明显的缺点就是,他的文档内容少的可怜。
而且国内的社区也不是很活跃。所以对使用这款框架的新书造成了很大困难。


作者作为一个入门也没多久的新手,尝试着从自己的角度,剖析一下这部框架的原理,讲述一下自己踩过的坑,同时也是监督自己的学习。

laravel框架的文档中的例子很多时候不是很明显,所以想要真正的使用好这个框架,我们可以尝试去阅读它源码中的注释(不得不说laravel源码的注释还是很详细的)。

新手解读:laravel 框架源码分析(一)_第1张图片

我们先来看一下laravel 的文件目录结构,上图为laravel官方给出的5.3版本目录结构,事实上laravel对目录结构的要求是松散的,你可以按照自己的需求,自由组合文件结构,关于各个文件夹的作用大家可以自行参考官方文档。

现在我们开始逐步剖析laravel!

首先与一般框架不同的是laravel的入口文件在public目录中,

新手解读:laravel 框架源码分析(一)_第2张图片

我们看到的index.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.
|
| Composer为我们的应用提供了非常方便的自动加载。我们可以很简单的引用脚本,避免了
| 我们去手动加载我们的类。
|
*/

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.
|
| 我们需要照亮PHP开发,所以让我们点亮这盏灯。这里自动加载了我们的框架以便使用,
| 然后我们可以加载我们的应用来为浏览器的请求返回响应并让我们的用户愉快的使用。
|
*/

$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.
|
| 一旦我们创建了应用,我们就可以通过核心服务来处理传入的请求,并且发送响应的
| 返回给浏览器。
|
*/

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

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

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

index.php文件的第一行代码就是就是

require __DIR__.'/../bootstrap/autoload.php';
laravel在这里引入了Composer提供的依赖注入,这样我们就无需手动加载任何类,有关Composer的内容与本文讨论的内容关联不大,有兴趣的小伙伴可以自行去 Composer文档自行查看。

第二行代码

$app = require_once __DIR__.'/../bootstrap/app.php';
laravel 引入了bootstrap/app.php文件,

这个文件是做什么的呢,我们可以来看一下app.php的内容:

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
);

/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;
在这里laravel创建了一个应用实例,在
Illuminate\Foundation\Application
中laravel注册了应用的基础绑定,接下来,laravel又向核心服务中注册了一部分重要的门面,最后返回应用的实例。




你可能感兴趣的:(laravel)