Request Lifecycle

Introduction

When using any tool in the "real world", you feel more confident if you understand how that tool works. Application development is no different. When you understand how your development tools function, you feel more comfortable and confident using them.

The goal of this document is to give you a good, high-level overview of how the Laravel framework "works". By getting to know the overall framework better, everything feels less "magical" and you will be more confident building your applications.

If you don't understand all of the terms right away, don't lose heart! Just try to get a basic grasp of what is going on, and your knowledge will grow as you explore other sections of the documentation.

当你更明白整个框架,所有的东西就不会在神奇, 你不很理解也没有关系,随着使用深入,你的知识会不断增长。

 

#生命周期概览

首要之事

The entry point for all requests to a Laravel application is the public/index.php file. All requests are directed to this file by your web server (Apache / Nginx) configuration. Theindex.php file doesn't contain much code. Rather, it is simply a starting point for loading the rest of the framework.

public/index.php 文件是对Laravel应用所有请求的进入点,所有请求都通过你的网页服务器(Apache/ Nginx )配置导向这个文件. Index.php 这个文件没有多少代码,它只是个起始点,去加载框架其他部分

The index.php file loads the Composer generated autoloader definition, and then retrieves an instance of the Laravel application from bootstrap/app.php script. The first action taken by Laravel itself is to create an instance of the application / service container.

Index.php 加载的是Composer生成的自动加载器定义, 然后接收一个由bootstrap/app.php 脚本产生的Laravel应用实例。 Laravel自身的第一个动作就是创建一个应用程序容器,或者说是服务容器实例。

HTTP / 终端核心

Next, the incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application. These two kernels serve as the central location that all requests flow through. For now, let's just focus on the HTTP kernel, which is located in app/Http/Kernel.php.

接下来,进入应用程序的请求会被送往HTTP核心,终端核心, 视请求的种类而定, 这两种核心是所有请求流向的中心位置, 现在我们只将焦点放在HTTP核心, 它位于app/Http/Kernel.php

 

The HTTP kernel extends the Illuminate\Foundation\Http\Kernel class, which defines an array ofbootstrappers 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.

HTTP核心扩展了Illuminate\Foundation\Http\Kernel 类, 它定义了一个bootstrappers 数组, 在请求被执行前会执行,这些启动器(bootstrapper)会配置, 错误处理,日志记录, 检测应用环境, 和其他请求被真正处理前需要完成的工作。

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 theHTTP session, determine if the application is in maintenance mode, verifying the CSRF token, and more.

HTTP核心也定义了一份HTTP中间件清单, 所有的请求在被应用程序处理之前都必须经过它们。 这些中间件有负责处理HTTP session的读写, 决定应用程序是否处于维护模式, 验证跨站请求伪造(CSRF)标记, 以及其他更多的功能。

The method signature for the HTTP kernel's handle method is quite simple: receive aRequest and return a Response. Think of the Kernel as being a big black box that represents your entire application. Feed it HTTP requests and it will return HTTP responses.

HTTP核心 handle 方法的签名相当简单: 它接收一个Request并返回一个Response. 把核心想象成一个大黑盒子,用来代表你整个的应用程序。 对它输入的HTTP请求, 它将返回HTTP响应。

 

服务提供者

One of the most important Kernel bootstrapping actions is loading the service providersfor your application. All of the service providers for the application are configured in theconfig/app.php configuration file's providers array. First, the register method will be called on all providers, then, once all providers have been registered, the boot method will be called.

最重要的核心启动行为之一,是加载您的应用程序的服务提供者。 所有的服务提供者,都在config/app.php 配置文件的 providers 数组中被配置。 首先, 对所有的提供者调用register 方法, 一旦所有的提供者都被注册后,boot 方法也会被调用。

Service providers are responsible for bootstrapping all of the framework's various components, such as the database, queue, validation, and routing components. Since they bootstrap and configure every feature offered by the framework, service providers are the most important aspect of the entire Laravel bootstrap process.

服务提供者负责启动所有框架的不同组件, 像是数据库database, 队列queue, 验证, 路由组件。因为它们启动和配置所有和框架有关的特性。 服务提供者是整个Laravel启动过程中最重要的地方。

 

请求分派

Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.

一旦应用程序启动且所有的服务提供者都被注册后, Request会被移交给路由器进行分派。 路由器会将请求分派给路由或控制器, 并执行任何特定路由的中间件。

 

#聚焦于服务提供者

Service providers are truly the key to bootstrapping a Laravel application. The application instance is created, the service providers are registered, and the request is handed to the bootstrapped application. It's really that simple!

服务提供者是启动Laravel应用程序的真正关键。创建应用实例,注册服务提供者, 将请求转至已经启动的应用程序。 真的就这么简单。

Having a firm grasp of how a Laravel application is built and bootstrapped via service providers is very valuable. Of course, your application's default service providers are stored in the app/Providers directory.

能确实掌握Laravel应用程序是如何建立,并通过服务提供者启动是很有价值的。 应用程序的默认服务提供者在 app/Providers 这个目录内。

By default, the AppServiceProvider is fairly empty. This provider is a great place to add your application's own bootstrapping and service container bindings. Of course, for large applications, you may wish to create several service providers, each with a more granular type of bootstrapping.

AppServiceProviders默认几乎是空的,此提供者是一个很好的地方,可以让你加入应用程序自身的启动及对服务容器的绑定。 当然,对于大型应用,你可以希望创建若干个服务提供者,每个都具备更精细的启动类型。

Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)的更多相关文章

  1. Laravel5.1学习笔记i14 系统架构6 Facade

    Facades 介绍  使用 Facades Facade 类参考   #介绍 Facades provide a "static" interface to classes th ...

  2. Laravel5.1学习笔记12 系统架构4 服务容器

    Service Container 介绍 绑定的用法  绑定实例到接口 上下文绑定 标签 解析 容器事件 #介绍 The Laravel service container is a powerful ...

  3. Laravel5.1学习笔记13 系统架构5 Contract

    Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...

  4. Laravel5.1学习笔记11 系统架构3 服务提供者

    服务提供者 简介 写一个服务提供者 Register注册方法 Boot 方法 注册提供者 缓载提供者 简介 Service providers are the central place of all ...

  5. Laravel5.1学习笔记10 系统架构2 应用程序结构

    应用程序结构 简介 根目录 App 目录 为应用程序设置命名空间 简介 默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始.当然,你可以依照喜好自由地组织应用 ...

  6. Android学习笔记(五)——活动的生命周期

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 为了能写出流畅连贯的程序,我们需要了解一下活动的生命周期. 一.返回栈 Android 中的活动是可以层叠的. ...

  7. 【React】学习笔记(二)——组件的生命周期、React脚手架使用

    原教程视频:ttps://www.bilibili.com/video/BV1wy4y1D7JT?p=2&spm_id_from=pageDriver 目录 一.组件的生命周期 1.1.生命周 ...

  8. Laravel5.1 学习笔记1, 目录结构和命名空间(待修)

    自从用 Laravel4做了个小网站,使用了数据库ORM Eloquent, 就放下了一段时间,想不到这个与Asp.net MVC 有着异曲同工之妙的框架已经出了下个版本,而且还有不小的改动,因此不得 ...

  9. Android学习笔记(十) Activity的生命周期

    一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...

随机推荐

  1. numpy.tile()

    numpy.tile()是个什么函数呢,说白了,就是把数组沿各个方向复制 比如 a = np.array([0,1,2]),    np.tile(a,(2,1))就是把a先沿x轴(就这样称呼吧)复制 ...

  2. 【XSY3413】Lambda - 造计算机初步——邱奇-图灵论题与lambda演算

    题意: 关于邱奇-图灵论题的一点思考 这道题起源于计算机科学史上一个非常著名的问题——邱奇-图灵论题,这个论题是可计算性理论的基石,关于它的思考与证明几乎贯穿了整个计算机科学史,涵盖了数学.算法理论. ...

  3. P1002 过河卒 【递推、简单动规】

    题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒 ...

  4. Linux系统学习之 一:新手必须掌握的Linux命令1

    2018-10-03 16:04:12 一.常用系统工作命令 1.wget 命令 作用:用于在终端中下载网络文件. 格式:wget [参数] 下载地址 参数及作用: -b : 后台下载模式 -d:显示 ...

  5. 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 3

    项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...

  6. linux学习9-进程管理知识

    Linux 进程管理 实验环境: 用户名:shiyanlou 密码:AJW3tui5 Linux进程之管理控制 实验介绍 通过本实验我们将掌握一些 Linux 所提供的工具来进行进程的查看与控制,掌握 ...

  7. jenkins简单持续构建

    一.安装jenkins 二.将需要持续构建的java project打包成jar文件 1.选择导出需要运行的main方法所在java类

  8. hdu_1012_u Calculate e_201310121519

    u Calculate eTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. Linux下统计某个目录的文件个数(转)

    1.统计某文件夹下文件个数,不包括子文件夹 比如:统计/home下.jpeg文件的个数 ls -l "/home" | grep ".jpeg" | wc -l ...

  10. MVC.Net:对MVC5部署时出现403.14错误的解决方法

    当我们部署MVC5到IIS 7的时候,有时会出现403.14的错误,如下图: 对于这个错误的解决方法就是在应用程序的web.config的system.webServer节点中加入这一句: <m ...