Providing The Core 核心也是服务提供者的模式

By now you have probably noticed that your application already has many service providers registered in the app configuration file. Each of these service providers bootstraps a part of the framework core. For example, the MigrationServiceProvider bootstraps the various classes that run migrations, as well as the migration Artisan commands. The EventServiceProvider bootstraps and registers the event dispatcher class. Some of these providers are larger than others, but they each bootstrap a piece of the core.

你可能已经注意到,在 app 配置文件里面已经有了很多服务提供者。每一个都负责启动框架核心的一部分。比如 MigrationServiceProvider 负责启动数据库迁移的类,包括 Artisan 里面的命令。EventServiceProvide 负责启动和注册事件调度机制。不同的服务提供者有着不同的复杂度,但他们都负责启动核心的一部分。

Meet Your Providers 和服务提供者们见见面

One of the best way to improve your understanding of the Laravel core is to read the source for the core service providers. If you are familiar with the function of service providers and what each core service provider registers, you will have an incredibly better understanding of how Laravel works under the hood.

理解 Laravel 核心的最好方法是去读它的核心服务源码。如果你对这些服务的源码、容器注册等都很熟悉,那么你对 Laravel 是如何工作的将会有十分深刻的理解。

Most of the core providers are deferred, meaning they do not load on every request; however, some, such as the FilesystemServiceProvider and ExceptionServiceProvider load on every request to your application since they bootstrap very foundational piece of the framework. One might say that the core service providers and the application container are Laravel. They are what ties all of the various pieces of the framework together into a single, cohesive unit. These providers are the building blocks of the framework.

大部分的服务提供者是延迟加载的,意味着并非所有请求都会调用到他们;然而有一些很基础的服务是每一次请求都会被加载的,比如 FilesystemServiceProvide 和 ExceptionServiceProvider 。有人会说核心服务提供者和应用程序容器就是 Laravel 。Laravel 其实是将这么多不同部分联系起来,形成一个单一的、内聚的整体的这么一个机制。拿建筑来比喻,那些服务提供者就是框架的预制模块。

As mentioned previously, if you wish to gain a deeper understanding of how the framework works, read the source code of the core service providers that ship with Laravel. By reading through these providers, you will gain a solid understanding of how the framework is put together, as well as what each provider offers to your application. Furthermore, you will be better prepared to contribute to Laravel itself!

正如之前提到的那样,如果你想更深的了解框架是如何运行的,请读 Lravel 的核心服务的源码吧。读过之后,你会对框架如何将各部分组合在一起、每一个服务是如何为你所用这些机制有更坚实的理解。此外,有了这些进一步的理解,你也可以为 Laravel 添砖加瓦!

results matching ""

    No results matching ""