ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
* * * * * [TOC] ## 简介 Laravel 的契约是一组定义框架提供的核心服务的接口。例如,`Illuminate\Contracts\Queue\Queue` 契约定义了队列任务所需的方法,而 `Illuminate\Contracts\Mail\Mailer` 契约定义了发送电子邮件所需的方法。 框架对每个契约都提供了相应的实现。例如,Laravel 提供了具有各种驱动的队列实现和由 [SwiftMailer](https://swiftmailer.symfony.com/) 提供支持的邮件驱动实现。 所有的 Laravel 契约都有 [他们自己的 GitHub 库](https://github.com/illuminate/contracts)。这为所有可用的契约提供了一个快速参考指南,同时也可单独作为低耦合的扩展包给其他包开发者使用。 ### 契约 VS Facades Laravel [Facades](https://www.kancloud.cn/tonyyu/laravel_5_6/786058) 和辅助函数提供了一种使用 Laravel 服务的简单方法,即不需要通过类型提示并从服务容器中解析契约。在大多数情况下,每个 Facades 都有一个等效的契约。 不像 Facades,不需要你在类的构造函数中类型提示,契约则需要你在类中明显地定义依赖项。一些开发者倾向于以契约这种方式明确地定义它们的依赖项,而其它开发者则更喜欢 Facades 带来的便捷。 > {tip} 对于大多数应用程序来说不管是使用 facades 还是契约都可以。但是,如果你正在构建一个扩展包,为了方便测试,你应该强烈考虑契约。 ## 何时使用契约 综上所述,使用契约或是 Facades 很大程度上归结于个人或者开发团队的喜好。不管是契约还是 Facades 都可以创建出健壮的、易测试的 Laravel 应用程序。如果你长期关注类的单一职责,你会注意到使用契约还是 Facades 其实没多少实际意义上的区别。 然而,你可能还是会有几个关于契约的问题。例如,为什么要使用接口?不使用接口会比较复杂吗?下面让我们谈下使用接口的原因:低耦合和简单性。 ### 低耦合 首先,让我们来看一些高耦合缓存实现的代码。如下: ~~~ <?php namespace App\Orders; class Repository { /** * 缓存实例。 */ protected $cache; /** * 创建一个仓库实例。 * * @param \SomePackage\Cache\Memcached $cache * @return void */ public function __construct(\SomePackage\Cache\Memcached $cache) { $this->cache = $cache; } /** * 按照 Id 检索订单 * * @param int $id * @return Order */ public function find($id) { if ($this->cache->has($id)) { // } } } ~~~ 在这个类中,程序跟给定的缓存实现高耦合。因为我们依赖于一个扩展包的特定缓存类。一旦这个扩展包的 API 被更改了,我们的代码就必须跟着改变。 同样的,如果我们想要将底层的的缓存技术( Memcached )替换为另一种缓存技术( Redis ),那又得再次修改这个 Repository 类。而 Repository 类不应该了解太多关于谁提供了这些数据或是如何提供的等等。 **比起上面的做法,我们可以使用一个简单的、与扩展包无关的接口来改进我们的代码:** ~~~ <?php namespace App\Orders; use Illuminate\Contracts\Cache\Repository as Cache; class Repository { /** * 缓存实例。 */ protected $cache; /** * 创建一个仓库实例。 * * @param Cache $cache * @return void */ public function __construct(Cache $cache) { $this->cache = $cache; } } ~~~ 现在,更改之后的代码没有与任何扩展包甚至是 Laravel 耦合。而契约扩展包不包含任何实现和依赖项,你可以轻松地写任何给定契约的替代实现,来实现不修改任何关于缓存消耗的代码就可以替换缓存实现。 ### 简单性 当所有 Laravel 的服务都使用简洁的接口定义,就很容易判断给定服务提供的功能。 **可以将契约视为说明框架功能的简洁文档。** 除此之外,当依赖的接口足够简洁时,代码的可读性和可维护性会大大提高。比起搜索一个大型复杂的类中有哪些可用的方法,不如检索一个简单、 干净的接口来参考更妥当。 ## 如何使用契约 那么,如何获得一个契约的实现呢?这其实很简单。 Laravel 中的许多类型的类都是通过 [服务容器](https://www.kancloud.cn/tonyyu/laravel_5_6/786056) 解析出来的,包括控制器、事件监听器、中间件、任务队列,甚至路由闭包。所以说,要获得一个契约的实现,你只需要被解析的类的构造函数中添加「类型提示」即可。 例如,看看这个事件监听器: ~~~ <?php namespace App\Listeners; use App\User; use App\Events\OrderWasPlaced; use Illuminate\Contracts\Redis\Database; class CacheOrderInformation { /** * Redis 数据库实现。 */ protected $redis; /** * 创建事件处理器实例。 * * @param Database $redis * @return void */ public function __construct(Database $redis) { $this->redis = $redis; } /** * 处理事件。 * * @param OrderWasPlaced $event * @return void */ public function handle(OrderWasPlaced $event) { // } } ~~~ 当事件监听器被解析时,服务容器会读取类的构造函数上的类型提示,并注入对应的值。想了解更多关于服务容器的注册,请查看 [这个文档](https://www.kancloud.cn/tonyyu/laravel_5_6/786056)。 ## 契约参考 下表提供了所有 Laravel 契约及其对应的 Facades: | 契约 | 参考 Facade | | --- | --- | | [Illuminate\Contracts\Auth\Access\Authorizable](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/Access/Authorizable.php) |    | | [Illuminate\Contracts\Auth\Access\Gate](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/Access/Gate.php) | `Gate` | | [Illuminate\Contracts\Auth\Authenticatable](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/Authenticatable.php) |    | | [Illuminate\Contracts\Auth\CanResetPassword](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/CanResetPassword.php) |   | | [Illuminate\Contracts\Auth\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/Factory.php) | `Auth` | | [Illuminate\Contracts\Auth\Guard](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/Guard.php) | `Auth::guard()` | | [Illuminate\Contracts\Auth\PasswordBroker](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/PasswordBroker.php) | `Password::broker()` | | [Illuminate\Contracts\Auth\PasswordBrokerFactory](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/PasswordBrokerFactory.php) | `Password` | | [Illuminate\Contracts\Auth\StatefulGuard](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/StatefulGuard.php) |   | | [Illuminate\Contracts\Auth\SupportsBasicAuth](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/SupportsBasicAuth.php) |   | | [Illuminate\Contracts\Auth\UserProvider](https://github.com/illuminate/contracts/blob/laravel/5.6/Auth/UserProvider.php) |   | | [Illuminate\Contracts\Bus\Dispatcher](https://github.com/illuminate/contracts/blob/laravel/5.6/Bus/Dispatcher.php) | `Bus` | | [Illuminate\Contracts\Bus\QueueingDispatcher](https://github.com/illuminate/contracts/blob/laravel/5.6/Bus/QueueingDispatcher.php) | `Bus::dispatchToQueue()` | | [Illuminate\Contracts\Broadcasting\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Broadcasting/Factory.php) | `Broadcast` | | [Illuminate\Contracts\Broadcasting\Broadcaster](https://github.com/illuminate/contracts/blob/laravel/5.6/Broadcasting/Broadcaster.php) | `Broadcast::connection()` | | [Illuminate\Contracts\Broadcasting\ShouldBroadcast](https://github.com/illuminate/contracts/blob/laravel/5.6/Broadcasting/ShouldBroadcast.php) |   | | [Illuminate\Contracts\Broadcasting\ShouldBroadcastNow](https://github.com/illuminate/contracts/blob/laravel/5.6/Broadcasting/ShouldBroadcastNow.php) |   | | [Illuminate\Contracts\Cache\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Cache/Factory.php) | `Cache` | | [Illuminate\Contracts\Cache\Lock](https://github.com/illuminate/contracts/blob/laravel/5.6/Cache/Lock.php) |   | | [Illuminate\Contracts\Cache\LockProvider](https://github.com/illuminate/contracts/blob/laravel/5.6/Cache/LockProvider.php) |   | | [Illuminate\Contracts\Cache\Repository](https://github.com/illuminate/contracts/blob/laravel/5.6/Cache/Repository.php) | `Cache::driver()` | | [Illuminate\Contracts\Cache\Store](https://github.com/illuminate/contracts/blob/laravel/5.6/Cache/Store.php) |   | | [Illuminate\Contracts\Config\Repository](https://github.com/illuminate/contracts/blob/laravel/5.6/Config/Repository.php) | `Config` | | [Illuminate\Contracts\Console\Application](https://github.com/illuminate/contracts/blob/laravel/5.6/Console/Application.php) |   | | [Illuminate\Contracts\Console\Kernel](https://github.com/illuminate/contracts/blob/laravel/5.6/Console/Kernel.php) | `Artisan` | | [Illuminate\Contracts\Container\Container](https://github.com/illuminate/contracts/blob/laravel/5.6/Container/Container.php) | `App` | | [Illuminate\Contracts\Cookie\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Cookie/Factory.php) | `Cookie` | | [Illuminate\Contracts\Cookie\QueueingFactory](https://github.com/illuminate/contracts/blob/laravel/5.6/Cookie/QueueingFactory.php) | `Cookie::queue()` | | [Illuminate\Contracts\Database\ModelIdentifier](https://github.com/illuminate/contracts/blob/laravel/5.6/Database/ModelIdentifier.php) |   | | [Illuminate\Contracts\Debug\ExceptionHandler](https://github.com/illuminate/contracts/blob/laravel/5.6/Debug/ExceptionHandler.php) |   | | [Illuminate\Contracts\Encryption\Encrypter](https://github.com/illuminate/contracts/blob/laravel/5.6/Encryption/Encrypter.php) | `Crypt` | | [Illuminate\Contracts\Events\Dispatcher](https://github.com/illuminate/contracts/blob/laravel/5.6/Events/Dispatcher.php) | `Event` | | [Illuminate\Contracts\Filesystem\Cloud](https://github.com/illuminate/contracts/blob/laravel/5.6/Filesystem/Cloud.php) | `Storage::cloud()` | | [Illuminate\Contracts\Filesystem\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Filesystem/Factory.php) | `Storage` | | [Illuminate\Contracts\Filesystem\Filesystem](https://github.com/illuminate/contracts/blob/laravel/5.6/Filesystem/Filesystem.php) | `Storage::disk()` | | [Illuminate\Contracts\Foundation\Application](https://github.com/illuminate/contracts/blob/laravel/5.6/Foundation/Application.php) | `App` | | [Illuminate\Contracts\Hashing\Hasher](https://github.com/illuminate/contracts/blob/laravel/5.6/Hashing/Hasher.php) | `Hash` | | [Illuminate\Contracts\Http\Kernel](https://github.com/illuminate/contracts/blob/laravel/5.6/Http/Kernel.php) |   | | [Illuminate\Contracts\Logging\Log](https://github.com/illuminate/contracts/blob/laravel/5.6/Logging/Log.php) | `Log` | | [Illuminate\Contracts\Mail\MailQueue](https://github.com/illuminate/contracts/blob/laravel/5.6/Mail/MailQueue.php) | `Mail::queue()` | | [Illuminate\Contracts\Mail\Mailable](https://github.com/illuminate/contracts/blob/laravel/5.6/Mail/Mailable.php) |   | | [Illuminate\Contracts\Mail\Mailer](https://github.com/illuminate/contracts/blob/laravel/5.6/Mail/Mailer.php) | `Mail` | | [Illuminate\Contracts\Notifications\Dispatcher](https://github.com/illuminate/contracts/blob/laravel/5.6/Notifications/Dispatcher.php) | `Notification` | | [Illuminate\Contracts\Notifications\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Notifications/Factory.php) | `Notification` | | [Illuminate\Contracts\Pagination\LengthAwarePaginator](https://github.com/illuminate/contracts/blob/laravel/5.6/Pagination/LengthAwarePaginator.php) |   | | [Illuminate\Contracts\Pagination\Paginator](https://github.com/illuminate/contracts/blob/laravel/5.6/Pagination/Paginator.php) |   | | [Illuminate\Contracts\Pipeline\Hub](https://github.com/illuminate/contracts/blob/laravel/5.6/Pipeline/Hub.php) |   | | [Illuminate\Contracts\Pipeline\Pipeline](https://github.com/illuminate/contracts/blob/laravel/5.6/Pipeline/Pipeline.php) |   | | [Illuminate\Contracts\Queue\EntityResolver](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/EntityResolver.php) |   | | [Illuminate\Contracts\Queue\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/Factory.php) | `Queue` | | [Illuminate\Contracts\Queue\Job](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/Job.php) |   | | [Illuminate\Contracts\Queue\Monitor](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/Monitor.php) | `Queue` | | [Illuminate\Contracts\Queue\Queue](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/Queue.php) | `Queue::connection()` | | [Illuminate\Contracts\Queue\QueueableCollection](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/QueueableCollection.php) |   | | [Illuminate\Contracts\Queue\QueueableEntity](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/QueueableEntity.php) |   | | [Illuminate\Contracts\Queue\ShouldQueue](https://github.com/illuminate/contracts/blob/laravel/5.6/Queue/ShouldQueue.php) |   | | [Illuminate\Contracts\Redis\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Redis/Factory.php) | `Redis` | | [Illuminate\Contracts\Routing\BindingRegistrar](https://github.com/illuminate/contracts/blob/laravel/5.6/Routing/BindingRegistrar.php) | `Route` | | [Illuminate\Contracts\Routing\Registrar](https://github.com/illuminate/contracts/blob/laravel/5.6/Routing/Registrar.php) | `Route` | | [Illuminate\Contracts\Routing\ResponseFactory](https://github.com/illuminate/contracts/blob/laravel/5.6/Routing/ResponseFactory.php) | `Response` | | [Illuminate\Contracts\Routing\UrlGenerator](https://github.com/illuminate/contracts/blob/laravel/5.6/Routing/UrlGenerator.php) | `URL` | | [Illuminate\Contracts\Routing\UrlRoutable](https://github.com/illuminate/contracts/blob/laravel/5.6/Routing/UrlRoutable.php) |   | | [Illuminate\Contracts\Session\Session](https://github.com/illuminate/contracts/blob/laravel/5.6/Session/Session.php) | `Session::driver()` | | [Illuminate\Contracts\Support\Arrayable](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/Arrayable.php) |   | | [Illuminate\Contracts\Support\Htmlable](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/Htmlable.php) |   | | [Illuminate\Contracts\Support\Jsonable](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/Jsonable.php) |   | | [Illuminate\Contracts\Support\MessageBag](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/MessageBag.php) |   | | [Illuminate\Contracts\Support\MessageProvider](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/MessageProvider.php) |   | | [Illuminate\Contracts\Support\Renderable](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/Renderable.php) |   | | [Illuminate\Contracts\Support\Responsable](https://github.com/illuminate/contracts/blob/laravel/5.6/Support/Responsable.php) |   | | [Illuminate\Contracts\Translation\Loader](https://github.com/illuminate/contracts/blob/laravel/5.6/Translation/Loader.php) |   | | [Illuminate\Contracts\Translation\Translator](https://github.com/illuminate/contracts/blob/laravel/5.6/Translation/Translator.php) | `Lang` | | [Illuminate\Contracts\Validation\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/Validation/Factory.php) | `Validator` | | [Illuminate\Contracts\Validation\ImplicitRule](https://github.com/illuminate/contracts/blob/laravel/5.6/Validation/ImplicitRule.php) |   | | [Illuminate\Contracts\Validation\Rule](https://github.com/illuminate/contracts/blob/laravel/5.6/Validation/Rule.php) |   | | [Illuminate\Contracts\Validation\ValidatesWhenResolved](https://github.com/illuminate/contracts/blob/laravel/5.6/Validation/ValidatesWhenResolved.php) |   | | [Illuminate\Contracts\Validation\Validator](https://github.com/illuminate/contracts/blob/laravel/5.6/Validation/Validator.php) | `Validator::make()` | | [Illuminate\Contracts\View\Engine](https://github.com/illuminate/contracts/blob/laravel/5.6/View/Engine.php) |   | | [Illuminate\Contracts\View\Factory](https://github.com/illuminate/contracts/blob/laravel/5.6/View/Factory.php) | `View` | | [Illuminate\Contracts\View\View](https://github.com/illuminate/contracts/blob/laravel/5.6/View/View.php) | `View::make()` |