zoukankan      html  css  js  c++  java
  • laravel 框架源码分析

    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(IlluminateContractsHttpKernel::class);

    $response = $kernel->handle(
    $request = IlluminateHttpRequest::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 IlluminateFoundationApplication(
    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(
    IlluminateContractsHttpKernel::class,
    AppHttpKernel::class
    );

    $app->singleton(
    IlluminateContractsConsoleKernel::class,
    AppConsoleKernel::class
    );

    $app->singleton(
    IlluminateContractsDebugExceptionHandler::class,
    AppExceptionsHandler::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创建了一个应用实例,在
    IlluminateFoundationApplication
    中laravel注册了应用的基础绑定,接下来,laravel又向核心服务中注册了一部分重要的门面,最后返回应用的实例。

    有需要交流的小伙伴可以点击这里加本人QQ:luke

  • 相关阅读:
    C#删除一个字符串数组中的空字符串
    .Net后台获取客户端信息
    Java Script
    ECMAScript闭包,ECMAScript对象
    Java Script函数、变量、对象
    JavaScript3
    JavaScript-2
    变量
    8.22收获
    html
  • 原文地址:https://www.cnblogs.com/starluke/p/11918788.html
Copyright © 2011-2022 走看看