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

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

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

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


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

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

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

我们看到的index.php就是我们框架的入口文件了,具体内容如下:

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| 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的内容:

<?php

/*
|--------------------------------------------------------------------------
| Create The Application
| 创建你的应用
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
| 我们首先要做的是创建一个新的Laravel应用实例以便我们将所有Laravel的组件都“粘在一起”,
| 并且这个的IoC容器绑定了Laravel的各个部分。
|
*/

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
| 接下来,我们需要向容器绑定一些重要的门面,以便我们能在需要的时候解决他们。
| 核心服务可以通过使用web和CLI处理进来的请求来响应应用。
*/

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

/*
|--------------------------------------------------------------------------
| 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又向核心服务中注册了一部分重要的门面,最后返回应用的实例。




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值