Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)

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

你可能感兴趣的:(laravel)