laravel
5. 深入探讨
契约

介绍

Laravel 的“契约”是一组接口,用于定义框架所提供的核心服务。例如,Illuminate\Contracts\Queue\Queue契约定义了排队任务所需的方法,而Illuminate\Contracts\Mail\Mailer契约定义了发送电子邮件所需的方法。

每个契约都有框架提供的相应实现。例如,Laravel 提供了具有多种驱动程序的队列实现,以及由 Symfony Mailer (opens in a new tab) 驱动的邮件发送器实现。

所有的 Laravel 契约都位于 它们自己的 GitHub 仓库 (opens in a new tab) 中。这为所有可用的契约提供了一个快速参考点,以及一个在构建与 Laravel 服务交互的包时可以使用的单一、解耦的包。

契约与外观模式

Laravel 的外观模式和辅助函数提供了一种无需在服务容器中进行类型提示和解析契约即可使用 Laravel 服务的简单方法。在大多数情况下,每个外观模式都有一个等效的契约。

与外观模式不同,外观模式不需要在类的构造函数中引入它们,而契约允许您为类定义明确的依赖关系。一些开发人员更喜欢以这种方式明确定义他们的依赖关系,因此更喜欢使用契约,而其他开发人员则喜欢外观模式的便利性。一般来说,大多数应用程序在开发过程中可以毫无问题地使用外观模式。

何时使用契约

使用契约还是外观模式的决定将取决于个人喜好和您的开发团队的喜好。契约和外观模式都可以用于创建强大、经过良好测试的 Laravel 应用程序。契约和外观模式并非相互排斥。您的应用程序的某些部分可能使用外观模式,而其他部分则依赖于契约。只要您保持类的职责集中,您会发现使用契约和外观模式之间的实际差异很小。

一般来说,大多数应用程序在开发过程中可以毫无问题地使用外观模式。如果您正在构建一个与多个 PHP 框架集成的包,您可能希望使用illuminate/contracts包来定义您与 Laravel 服务的集成,而无需在您的包的composer.json文件中要求 Laravel 的具体实现。

如何使用契约

那么,如何获得契约的实现呢?实际上非常简单。

Laravel 中的许多类型的类都是通过服务容器进行解析的,包括控制器、事件监听器、中间件、排队任务,甚至路由闭包。因此,要获得契约的实现,您可以在正在解析的类的构造函数中“类型提示”该接口。

例如,看看这个事件监听器:

<?php

namespace App\Listeners;

use App\Events\OrderWasPlaced;
use App\Models\User;
use Illuminate\Contracts\Redis\Factory;

class CacheOrderInformation
{
    /**
     * 创建一个新的事件处理程序实例。
     */
    public function __construct(
        protected Factory $redis,
    ) {}

    /**
     * 处理事件。
     */
    public function handle(OrderWasPlaced $event): void
    {
        //...
    }
}

当解析事件监听器时,服务容器将读取类的构造函数中的类型提示,并注入适当的值。要了解更多关于在服务容器中注册内容的信息,请查看其文档

契约参考

此表提供了所有 Laravel 契约及其等效外观的快速参考:

契约参考外观
Illuminate\Contracts\Auth\Access\Authorizable (opens in a new tab)
Illuminate\Contracts\Auth\Access\Gate (opens in a new tab)Gate
Illuminate\Contracts\Auth\Authenticatable (opens in a new tab)
Illuminate\Contracts\Auth\CanResetPassword (opens in a new tab)
Illuminate\Contracts\Auth\Factory (opens in a new tab)Auth
Illuminate\Contracts\Auth\Guard (opens in a new tab)Auth::guard()
Illuminate\Contracts\Auth\PasswordBroker (opens in a new tab)Password::broker()
Illuminate\Contracts\Auth\PasswordBrokerFactory (opens in a new tab)Password
Illuminate\Contracts\Auth\StatefulGuard (opens in a new tab)
Illuminate\Contracts\Auth\SupportsBasicAuth (opens in a new tab)
Illuminate\Contracts\Auth\UserProvider (opens in a new tab)
Illuminate\Contracts\Broadcasting\Broadcaster (opens in a new tab)Broadcast::connection()
Illuminate\Contracts\Broadcasting\Factory (opens in a new tab)Broadcast
Illuminate\Contracts\Broadcasting\ShouldBroadcast (opens in a new tab)
Illuminate\Contracts\Broadcasting\ShouldBroadcastNow (opens in a new tab)
Illuminate\Contracts\Bus\Dispatcher (opens in a new tab)Bus
Illuminate\Contracts\Bus\QueueingDispatcher (opens in a new tab)Bus::dispatchToQueue()
Illuminate\Contracts\Cache\Factory (opens in a new tab)Cache
Illuminate\Contracts\Cache\Lock (opens in a new tab)
Illuminate\Contracts\Cache\LockProvider (opens in a new tab)
Illuminate\Contracts\Cache\Repository (opens in a new tab)Cache::driver()
Illuminate\Contracts\Cache\Store (opens in a new tab)
Illuminate\Contracts\Config\Repository (opens in a new tab)Config
Illuminate\Contracts\Console\Application (opens in a new tab)
Illuminate\Contracts\Console\Kernel (opens in a new tab)Artisan
Illuminate\Contracts\Container\Container (opens in a new tab)App
Illuminate\Contracts\Cookie\Factory (opens in a new tab)Cookie
Illuminate\Contracts\Cookie\QueueingFactory (opens in a new tab)Cookie::queue()
Illuminate\Contracts\Database\ModelIdentifier (opens in a new tab)
Illuminate\Contracts\Debug\ExceptionHandler (opens in a new tab)
Illuminate\Contracts\Encryption\Encrypter (opens in a new tab)Crypt
Illuminate\Contracts\Events\Dispatcher (opens in a new tab)Event
Illuminate\Contracts\Filesystem\Cloud (opens in a new tab)Storage::cloud()
Illuminate\Contracts\Filesystem\Factory (opens in a new tab)Storage
Illuminate\Contracts\Filesystem\Filesystem (opens in a new tab)Storage::disk()
Illuminate\Contracts\Foundation\Application (opens in a new tab)App
Illuminate\Contracts\Hashing\Hasher (opens in a new tab)Hash
Illuminate\Contracts\Http\Kernel (opens in a new tab)
Illuminate\Contracts\Mail\Mailable (opens in a new tab)
Illuminate\Contracts\Mail\Mailer (opens in a new tab)Mail
Illuminate\Contracts\Mail\MailQueue (opens in a new tab)Mail::queue()
Illuminate\Contracts\Notifications\Dispatcher (opens in a new tab)Notification
Illuminate\Contracts\Notifications\Factory (opens in a new tab)Notification
Illuminate\Contracts\Pagination\LengthAwarePaginator (opens in a new tab)
Illuminate\Contracts\Pagination\Paginator (opens in a new tab)
Illuminate\Contracts\Pipeline\Hub (opens in a new tab)
Illuminate\Contracts\Pipeline\Pipeline (opens in a new tab)Pipeline
Illuminate\Contracts\Queue\EntityResolver (opens in a new tab)
Illuminate\Contracts\Queue\Factory (opens in a new tab)Queue
Illuminate\Contracts\Queue\Job (opens in a new tab)
Illuminate\Contracts\Queue\Monitor (opens in a new tab)Queue
Illuminate\Contracts\Queue\Queue (opens in a new tab)Queue::connection()
Illuminate\Contracts\Queue\QueueableCollection (opens in a new tab)
Illuminate\Contracts\Queue\QueueableEntity (opens in a new tab)
Illuminate\Contracts\Queue\ShouldQueue (opens in a new tab)
Illuminate\Contracts\Redis\Factory (opens in a new tab)Redis
Illuminate\Contracts\Routing\BindingRegistrar (opens in a new tab)Route
Illuminate\Contracts\Routing\Registrar (opens in a new tab)Route
Illuminate\Contracts\Routing\ResponseFactory (opens in a new tab)Response
Illuminate\Contracts\Routing\UrlGenerator (opens in a new tab)URL
Illuminate\Contracts\Routing\UrlRoutable (opens in a new tab)
Illuminate\Contracts\Session\Session (opens in a new tab)Session::driver()
Illuminate\Contracts\Support\Arrayable (opens in a new tab)
Illuminate\Contracts\Support\Htmlable (opens in a new tab)
Illuminate\Contracts\Support\Jsonable (opens in a new tab)
Illuminate\Contracts\Support\MessageBag (opens in a new tab)
Illuminate\Contracts\Support\MessageProvider (opens in a new tab)
Illuminate\Contracts\Support\Renderable (opens in a new tab)
Illuminate\Contracts\Support\Responsable (opens in a new tab)
Illuminate\Contracts\Translation\Loader (opens in a new tab)
Illuminate\Contracts\Translation\Translator (opens in a new tab)Lang
Illuminate\Contracts\Validation\Factory (opens in a new tab)Validator
Illuminate\Contracts\Validation\ValidatesWhenResolved (opens in a new tab)
Illuminate\Contracts\Validation\ValidationRule (opens in a new tab)
Illuminate\Contracts\Validation\Validator (opens in a new tab)Validator::make()
Illuminate\Contracts\View\Engine (opens in a new tab)
Illuminate\Contracts\View\Factory (opens in a new tab)View
Illuminate\Contracts\View\View (opens in a new tab)View::make()