为什么80%的码农都做不了架构师?>>>   

上一篇

As Organizer 作为管理工具

One of the keys to building a well architected Laravel application is learning to use service providers as an organizational tool. When you are registering many classes with the IoC container, all of those bindings can start to clutter your app/start files. Instead of doing container registerations in those files, create service providers that register related services.

想制作一个结构优美的Laravel应用的话,就要去学习如何用服务提供者来管理代码。当你在注册IoC绑定的时候,所有代码都杂乱的塞进了app/start路径下的文件里。 别再这样做了,使用服务提供者来注册这些吧。

Get It Started 万物之初

Your application's "start" files are all stored in the app/start directory. These are "bootstrap" files that are loaded based on the type of the request entering your application. Start files named after an environment are loaded after the global start.php file. Finally, the artisan.php start file is loaded when any console command is executed.

你应用的“启动”文件都储存在app/start目录下。根据不同的请求入口,系统会载入不同的启动文件。在全局的start.php文件加载后,系统会根据执行环境的不同来加载不同的启动文件。 此外,在执行命令行程序时,artisan.php文件会被载入。

Let's explore an example. Perhaps our application is using Pusher to push messages to clients via WebSockets. In order to decouple ourselves from Pusher, it will be benificial to create an EventPusherInterface, and a PusherEventPusher implementation. This will allow us to easily change WebSocket providers down the road as requirements change or our application grows.

咱们来考虑这个例子。也许我们的应用正在使用Pusher 来为客户推送消息。为了将我们的应用和Pusher解耦,我们要定义EventPusherInterface接口和对应的实现类PusherEventPusher。这样在需求变化或应用改进时,我们就可以随时轻松的改变推送服务提供商。

<!-- lang: php -->
interface EventPusherInterface{public function push($message, array $data = array());
}class PusherEventPusher implements EventPusherInterface{public function __construct(PusherSdk $pusher){$this->pusher = $pusher;}public function push($message, array $data = array()){// Push message via the Pusher SDK...}
}

Next, let's create an EventPusherServiceProvider:

接下来我们创建一个EventPusherServiceProvider

<!-- lang:php -->
use Illuminate\Support\ServiceProvider;class EventPusherServiceProvider extends ServiceProvider {public function register(){$this->app->singleton('PusherSdk', function(){return new PusherSdk('app-key', 'secret-key');}$this->app->singleton('EventPusherInterface', 'PusherEventPusher');}
}

Great! Now we have a clean abstraction over event pushing, as well as a convenient place to register this, and other related bindings, in the container. Finally, we just need to add the EventPusherServiceProvider to our providers array in the app/config/app.php configuration file. Now we are ready to inject the EventPusherInterface into any controller or class within our application.

很好! 我们对事件推送进行了清晰的抽象,同时我们也有了一个很不错的地方进行注册、绑定其他相关的东西到容器里。最后一步只需要将EventPusherServiceProvider写入app/config/app.php文件内的providers数组里就可以了。现在这个应用里的EventPusherInterface已经被绑定到了正确的实现类上。

Should You Singleton? 要使用单例么?

You will need to evaluate whether to bind classes with bind or singleton. If you only want one instance of the class to be created per request cycle, use singleton. Otherwise, use bind.

用不用单例可以这样来考虑:如果在一次请求周期中该类只需要有一个实例,就使用singleton;否则就使用bind

Note that a service provider has an $app instance available via the base ServiceProvider class. This is a full Illuminate\Foundation\Application instance, which inherits from the Container class, so we can call all of the IoC container methods we are used to. If you preffer to use the App facade inside the service provider, you may do that as well:

    App::singleton('EventPusherInterface', 'PusherEventPusher');

Of course, service providers are not limited to registering certain kinds of services. We could use them to register our cloud file storage services, database access services, a custom view engine such as Twig, etc. They are simply bootstrapping and organizational tools for your application. Nothing more.

当然服务提供者的功能不仅仅局限于消息推送。像是云存储、数据库访问、自定义的视图引擎比如Twig等等都可以用这种模式来设置。服务提供者就是你的应用里的启动代码和管理工具,没什么神奇的。

So, don't be scared to create your own service providers. They are not something that should be strictly limited to distributed packages, but rather are great organizational tools for your own applications. Be creative and use them to bootstrap your various application components.

所以大胆的去创建你自己的服务提供者。并不是你非要发布个什么软件包才需要服务提供者,他们只是非常好的管理代码的工具。使用它们的力量去管理好应用中的各个组件吧。

下一篇

转载于:https://my.oschina.net/zgldh/blog/353989

From Apprentice To Artisan 翻译 08相关推荐

  1. From Apprentice To Artisan 翻译 19

    为什么80%的码农都做不了架构师?>>>    上一篇 Interface Segregation Principle 接口隔离原则 Introduction 介绍 The Inte ...

  2. From Apprentice To Artisan 翻译 17

    为什么80%的码农都做不了架构师?>>>    上一篇 Open Closed Principle 开放封闭原则 Introduction 介绍 Over the life time ...

  3. 计算机专业英语影印版单词,计算机专业英语词汇词组翻译(08影印版)

    UNIT 1 application software 应用软件 basic application 基础程序 communication device 通讯设备 compact disc(CD) 高 ...

  4. Laravel深入学习5 - 应用架构

    声明:本文并非博主原创,而是来自对<Laravel 4 From Apprentice to Artisan>阅读的翻译和理解,当然也不是原汁原味的翻译,能保证90%的原汁性,另外因为是理 ...

  5. Laravel深入学习10 - 里氏替换原则

    声明:本文并非博主原创,而是来自对<Laravel 4 From Apprentice to Artisan>阅读的翻译和理解,当然也不是原汁原味的翻译,能保证90%的原汁性,另外因为是理 ...

  6. php 单一职责原则,Laravel深入学习8 - 单一责任原则

    声明:本文并非博主原创,而是来自对<Laravel 4 From Apprentice to Artisan>阅读的翻译和理解,当然也不是原汁原味的翻译,能保证90%的原汁性,另外因为是理 ...

  7. php lumen和laravel,Laravel 还是 Lumen?

    Laravel 还是 Lumen?相信有不少人在纠结这个问题,相对来讲,我并不推荐使用 Lumen,因为作者的更新维护很明显还是偏重于 Laravel 的,还有一个理由就是 Laravel 已经包含了 ...

  8. picoCTF,Forensics,取证类,43/50

    picoCTF,Forensics,43/50 2019 picoCTF 01.Glory of the Garden,50分 02.extensions,150分 03.shark on wire ...

  9. 6. Laravel5学习笔记:IOC/DI的理解

    介绍 IOC 控制反转 Inversion of Control 依赖关系的转移 依赖抽象而非实践 DI 依赖注入 Dependency Injection 不必自己在代码中维护对象的依赖 容器自动根 ...

最新文章

  1. 华为诺亚方舟加拿大实验室提出BANet,双向视觉注意力机制用于单目相机深度估计...
  2. python——装饰器
  3. numeric column can contains null
  4. [Python从零到壹] 三十七.图像处理基础篇之图像融合处理和ROI区域绘制
  5. [android] No resource found that matches the given name 'Theme.AppCompat.Light'
  6. HDU - 4280 Island Transport(最大流)
  7. haskell vscode下的环境搭配(包含各种坑的解决办法)
  8. Java面试 - List和Set比较,各自的子类比较
  9. mysql如何添加用户_如何创建新用户和授予MySQL中的权限
  10. 微软 Build 2019 对开发者意味着什么?
  11. 实验3-2 计算符号函数的值 (10 分)
  12. VMware共享文件夹Input/output error解决办法
  13. 深入浅出Python——Python高级语法之文件操作
  14. 2020ICPC 昆明热身赛 C.Statues(小思维)
  15. mysql数据库加密方法l_使用透明数据库加密
  16. 寻找http://localhost/phpmyadmin出现的问题:HTTP 错误 404.0 - Not Found 您要找的资源已被删除、已更名或暂时不可用
  17. 《GPU编程与CG语言之阳春白雪下里巴人》阅读笔记 第五章+第六章
  18. 读HTTP权威指南的体会
  19. qq群 发言统计for tc
  20. GEE学习笔记 六十七:【GEE之Python版教程一】GEE学习背景介绍

热门文章

  1. python3.7官网下载步骤_python下载(python官网下载步骤)
  2. IDEA报错 com.microsoft.sqlserver.jdbc.SQLServerException : 列名或所提供的数目与表定于不匹配
  3. vim 安装_vim实战:插件安装(Vundle,NerdTree)
  4. Postman Request Payload发送请求
  5. json 数组 select默认选中
  6. python爬取去哪网数据_Python爬虫入门:使用Python爬取网络数据
  7. n阶方阵的蛇形排列java_「P·R·N·D」的排列顺序为何成为行业标准,能不能改变呢?...
  8. python多维数组添加元素_numpy中三维数组中加入元素后的位置详解
  9. iis7连接mysql_windows2008中正确使用IIS7配置PHP与MySQL
  10. java 文本词频统计_Java实现中文词频统计