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. String s="a"+"b"+"c"+"d";创建了几个对象?

    对于如下代码: package reviewTest; /** * @ClassName: StringTest * @Description: 测试String的字符串相加优化 * @author ...

  2. 【转载】xShell5 利用 sftp 在本地和服务器之间传输文件

    sftp是Secure File TransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...

  3. CentOS6.8 安装python2.7,pip以及yum

    由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...

  4. JavaScript学习笔记之DOM对象

    目录 1.Document 2.Element 3.Attribute 4.Event 1.Document 每个载入浏览器的 HTML 文档都会成为 Document 对象,Document 对象允 ...

  5. NLTK学习笔记(六):利用机器学习进行文本分类

    目录 一.监督式分类:建立在训练语料基础上的分类 特征提取器和朴素贝叶斯分类器 过拟合:当特征过多 错误分析 二.实例:文本分类和词性标注 文本分类 词性标注:"决策树"分类器 三 ...

  6. 【ACM】hdu_zs3_1006_AB_201308101123

    A/B Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submission ...

  7. href=#与 href=javascript:void(0) 的区别

    <a href="#"> 点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP <a href="javascript:void(0)" ...

  8. ZooKeeper之初识

    它是什么 俗称动物管理员,它使用java开发,开源,接口简单,高效,稳定的分布式系统,为其它分布式系统提供协调服务 为什么会存在? 开发分布式系统跟单机上做开发完全不同,碰到的问题完全不同,开发分布式 ...

  9. not in 和 <> 不走索引

    首先我们要知道的一点就是CBO的代码oracle是不会对我们公开的,起码现在是.所以本文中的结论不一定适用所有的版本.在应用本文的结论之前最好先试一下. ok 下面就是本文的结论,当你在where语句 ...

  10. springmvc 解析xml数据

    springmvc 解析xml数据 http://blog.csdn.net/zhi_jun/article/details/37925475