zoukankan      html  css  js  c++  java
  • Laravel5.1 启动详解

    借鉴:http://www.cnblogs.com/wish123/p/4756669.html

    Laravel所有请求的入口文件是:/public/index.php,代码如下

    <?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.
    |
    */
    
    //自动加载文件设置 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. | */
    //初始化服务容器,即框架的 IoC 容器,生成一个Laravel应用实例 $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. | */
    //通过服务容器生成一个 HTTP / Console kernel类的实例 $kernel = $app->make(IlluminateContractsHttpKernel::class);
    //kernel类实例运行handle方法,接收请求和处理结果(主要是运行middleware和URL相关的controller)
    $response = $kernel->handle( $request = IlluminateHttpRequest::capture() );
    //返回处理结果
    $response->send();
    //为整个请求处理周期展示最后的任何想要提供的动作,如每次访问后想要记录用户行为、给前端发消息
    $kernel->terminate($request, $response);

    (1)自动加载文件

      自动加载文件功能是通过Composer实现的,在composer.json中设置的加载方式里提取加载路径,然后根据Laravel对应的加载方式进行处理,主要为四种加载方式:

    1. PSR0加载方式—对应的文件就是autoload_namespaces.php
    2. PSR4加载方式—对应的文件就是autoload_psr4.php
    3. 其他加载类的方式—对应的文件就是autoload_classmap.php
    4. 加载公用方法—对应的文件就是autoload_files.php

      具体的定义形式在 composer.json 里面设置路径,如下:

    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\": "app/",
            "App\Http\Models\": "app/Http/Models/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },

    (2)初始化服务容器

      The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection.

      这句话是指 Laravel 服务容器的核心是依赖注入(也是控制反转,一种设计模式),它是把在类里面定义实例的这种高耦合模式摒弃掉,虽然工厂模式也行,不过也比较不灵活,然后以一种依赖的方式去把相关的类注册在容器里,需要时再把类实例从容器提取出来注入到本身。

      本质上是分离,将各种功能分离开,保证了松耦合,例如应用程序用到Foo类,Foo类需要Bar类,Bar类需要Bim类,那么先创建Bim类,再创建Bar类并把Bim注入,再创建Foo类,并把Bar类注入,再调用Foo方法,Foo调用Bar方法,接着做些其它工作。

      Laravel这个过程粗略的有以下几个步骤:

      1、registerBaseBindings:初始化基本的容器实例。

      2、registerBaseServiceProviders:注册基本的service provider,有两个:event、route。event service provider会设置队列的处理工厂,route service provider定义一些路由行为,包括请求、反应、重定向这些。

      3、registerCoreContainerAliases:注册核心的容器功能类的别名,这些类包括验证、缓存、配置、队列、session等。

    (3)生成kernel类

      The HTTP kernel extends the IlluminateFoundationHttpKernel class, which defines an array of bootstrappers that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled.

      The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determine if the application is in maintenance mode, verifying the CSRF token, and more.

    (4)kernel handle处理

      将kernel当成黑盒来看,接收请求、处理请求。

  • 相关阅读:
    log4j
    JDBCtemplete 模板
    动态代理 aop切面实现事务管理
    spring
    spring mvc 简单实现及相关配置实现
    ssm整合
    Jquery
    Git分布式版本控制系统
    Java web server 基本实现原理
    jvm
  • 原文地址:https://www.cnblogs.com/linguoguo/p/5626066.html
Copyright © 2011-2022 走看看