イントロダクションIntroduction
Laravelの「契約」は、フレームワークが提供するコアサービスを定義する一連のインターフェイスです。たとえば、Illuminate\Contracts\Queue\Queue
契約は、ジョブのキューイングに必要なメソッドを定義し、Illuminate\Contracts\Mail\Mailer
契約は、メールの送信に必要なメソッドを定義します。Laravel's "contracts" are a set of interfaces that define the core services provided by the framework. For example, an Illuminate\Contracts\Queue\Queue
contract defines the methods needed for queueing jobs, while the Illuminate\Contracts\Mail\Mailer
contract defines the methods needed for sending e-mail.
各契約は対応する、フレームワークが提供する実装を持っています。例えば、Laravelでは、様々なドライバを持つキューの実装と、Symfony Mailerを利用したメーラーの実装が提供されています。Each contract has a corresponding implementation provided by the framework. For example, Laravel provides a queue implementation with a variety of drivers, and a mailer implementation that is powered by Symfony Mailer[https://symfony.com/doc/6.0/mailer.html].
すべてのLaravel契約は、独自のGitHubリポジトリにあります。これは、利用可能なすべての契約のクイックリファレンスポイントと、Laravelサービスと相互関係するパッケージを構築するときに利用できる単一の分離されたパッケージを提供しています。All of the Laravel contracts live in their own GitHub repository[https://github.com/illuminate/contracts]. This provides a quick reference point for all available contracts, as well as a single, decoupled package that may be utilized when building packages that interact with Laravel services.
契約対ファサードContracts Vs. Facades
Laravelのファサードとヘルパ関数は、サービスコンテナから契約を入力して解決する必要なしに、Laravelのサービスを利用する簡単な方法を提供します。ほとんどの場合、各ファサードには同等の契約があります。Laravel's facades[/docs/{{version}}/facades] and helper functions provide a simple way of utilizing Laravel's services without needing to type-hint and resolve contracts out of the service container. In most cases, each facade has an equivalent contract.
クラスのコンストラクターでファサードをリクエストする必要がないファサードとは異なり、契約を使用すると、クラスの明示的な依存関係を定義できます。一部の開発者は、この方法で依存関係を明示的に定義することを好み、したがって契約を使用することを好みますが、他の開発者はファサードの便利さを楽しんでいます。一般に、ほとんどのアプリケーションは開発中に問題なくファサードを使用できます。Unlike facades, which do not require you to require them in your class' constructor, contracts allow you to define explicit dependencies for your classes. Some developers prefer to explicitly define their dependencies in this way and therefore prefer to use contracts, while other developers enjoy the convenience of facades. In general, most applications can use facades without issue during development.
契約を使うときWhen To Use Contracts
契約またはファサードを使用するかどうかの決定は、個人的な好みと開発チームの好みに依存します。契約とファサードの両方を使用して、堅牢で十分にテストされたLaravelアプリケーションを作成できます。契約とファサードは相互に排他的ではありません。アプリケーションの一部はファサードを使用し、他の部分は契約に依存できます。クラスの責任を集中させている限り、契約とファサードの使用の実際的な違いはほとんどありません。The decision to use contracts or facades will come down to personal taste and the tastes of your development team. Both contracts and facades can be used to create robust, well-tested Laravel applications. Contracts and facades are not mutually exclusive. Some parts of your applications may use facades while others depend on contracts. As long as you are keeping your class' responsibilities focused, you will notice very few practical differences between using contracts and facades.
一般に、ほとんどのアプリケーションは、開発中に問題なくファサードを使用できます。複数のPHPフレームワークと統合するパッケージを構築している場合は、パッケージのcomposer.json
ファイルにLaravelの具体的な実装をリクエストすることなく、illuminate/contracts
パッケージを使用してLaravelのサービスとの統合を定義することをお勧めします。In general, most applications can use facades without issue during development. If you are building a package that integrates with multiple PHP frameworks you may wish to use the illuminate/contracts
package to define your integration with Laravel's services without the need to require Laravel's concrete implementations in your package's composer.json
file.
契約の使用法How To Use Contracts
では、どのようにして契約を実装するのでしょう?本当にとても簡単です。So, how do you get an implementation of a contract? It's actually quite simple.
Laravelの多種のクラスは、サービスコンテナを介して解決されます。これには、コントローラ、イベントリスナ、ミドルウェア、キュー投入するジョブ、さらにルートクロージャまでも含みます。したがって、契約の実装を取得するには、解決されるクラスのコンストラクターでインターフェイスを「タイプヒント」するだけです。Many types of classes in Laravel are resolved through the service container[/docs/{{version}}/container], including controllers, event listeners, middleware, queued jobs, and even route closures. So, to get an implementation of a contract, you can just "type-hint" the interface in the constructor of the class being resolved.
たとえば、以下のイベントリスナを見てください。For example, take a look at this event listener:
<?php
namespace App\Listeners;
use App\Events\OrderWasPlaced;
use App\Models\User;
use Illuminate\Contracts\Redis\Factory;
class CacheOrderInformation
{
/**
* Redisファクトリの実装
*
* @var \Illuminate\Contracts\Redis\Factory
*/
protected $redis;
/**
* 新しいイベントハンドラインスタンスの生成
*
* @param \Illuminate\Contracts\Redis\Factory $redis
* @return void
*/
public function __construct(Factory $redis)
{
$this->redis = $redis;
}
/**
* イベントの処理
*
* @param \App\Events\OrderWasPlaced $event
* @return void
*/
public function handle(OrderWasPlaced $event)
{
//
}
}
このイベントリスナが依存解決されるとき、サービスコンテナはクラスコンストラクタのタイプヒントを読み取り、適切な値を依存注入します。サービスコンテナへの登録の詳細は、ドキュメントをご覧ください。When the event listener is resolved, the service container will read the type-hints on the constructor of the class, and inject the appropriate value. To learn more about registering things in the service container, check out its documentation[/docs/{{version}}/container].
契約リファレンスContract Reference
この一覧表は、すべてのLaravel契約とそれに該当するファサードのクイックリファレンスです。This table provides a quick reference to all of the Laravel contracts and their equivalent facades:
契約Contract | 参照ファサードReferences Facade |
---|---|
Illuminate\Contracts\Auth\Access\AuthorizableIlluminate\Contracts\Auth\Access\Authorizable[https://github.com/illuminate/contracts/blob/{{version}}/Auth/Access/Authorizable.php] | |
Illuminate\Contracts\Auth\Access\GateIlluminate\Contracts\Auth\Access\Gate[https://github.com/illuminate/contracts/blob/{{version}}/Auth/Access/Gate.php] | Gate Gate |
Illuminate\Contracts\Auth\AuthenticatableIlluminate\Contracts\Auth\Authenticatable[https://github.com/illuminate/contracts/blob/{{version}}/Auth/Authenticatable.php] | |
Illuminate\Contracts\Auth\CanResetPasswordIlluminate\Contracts\Auth\CanResetPassword[https://github.com/illuminate/contracts/blob/{{version}}/Auth/CanResetPassword.php] | |
Illuminate\Contracts\Auth\FactoryIlluminate\Contracts\Auth\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Auth/Factory.php] | Auth Auth |
Illuminate\Contracts\Auth\GuardIlluminate\Contracts\Auth\Guard[https://github.com/illuminate/contracts/blob/{{version}}/Auth/Guard.php] | Auth::guard() Auth::guard() |
Illuminate\Contracts\Auth\PasswordBrokerIlluminate\Contracts\Auth\PasswordBroker[https://github.com/illuminate/contracts/blob/{{version}}/Auth/PasswordBroker.php] | Password::broker() Password::broker() |
Illuminate\Contracts\Auth\PasswordBrokerFactoryIlluminate\Contracts\Auth\PasswordBrokerFactory[https://github.com/illuminate/contracts/blob/{{version}}/Auth/PasswordBrokerFactory.php] | Password Password |
Illuminate\Contracts\Auth\StatefulGuardIlluminate\Contracts\Auth\StatefulGuard[https://github.com/illuminate/contracts/blob/{{version}}/Auth/StatefulGuard.php] | |
Illuminate\Contracts\Auth\SupportsBasicAuthIlluminate\Contracts\Auth\SupportsBasicAuth[https://github.com/illuminate/contracts/blob/{{version}}/Auth/SupportsBasicAuth.php] | |
Illuminate\Contracts\Auth\UserProviderIlluminate\Contracts\Auth\UserProvider[https://github.com/illuminate/contracts/blob/{{version}}/Auth/UserProvider.php] | |
Illuminate\Contracts\Bus\DispatcherIlluminate\Contracts\Bus\Dispatcher[https://github.com/illuminate/contracts/blob/{{version}}/Bus/Dispatcher.php] | Bus Bus |
Illuminate\Contracts\Bus\QueueingDispatcherIlluminate\Contracts\Bus\QueueingDispatcher[https://github.com/illuminate/contracts/blob/{{version}}/Bus/QueueingDispatcher.php] | Bus::dispatchToQueue() Bus::dispatchToQueue() |
Illuminate\Contracts\Broadcasting\FactoryIlluminate\Contracts\Broadcasting\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Broadcasting/Factory.php] | Broadcast Broadcast |
Illuminate\Contracts\Broadcasting\BroadcasterIlluminate\Contracts\Broadcasting\Broadcaster[https://github.com/illuminate/contracts/blob/{{version}}/Broadcasting/Broadcaster.php] | Broadcast::connection() Broadcast::connection() |
Illuminate\Contracts\Broadcasting\ShouldBroadcastIlluminate\Contracts\Broadcasting\ShouldBroadcast[https://github.com/illuminate/contracts/blob/{{version}}/Broadcasting/ShouldBroadcast.php] | |
Illuminate\Contracts\Broadcasting\ShouldBroadcastNowIlluminate\Contracts\Broadcasting\ShouldBroadcastNow[https://github.com/illuminate/contracts/blob/{{version}}/Broadcasting/ShouldBroadcastNow.php] | |
Illuminate\Contracts\Cache\FactoryIlluminate\Contracts\Cache\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Cache/Factory.php] | Cache Cache |
Illuminate\Contracts\Cache\LockIlluminate\Contracts\Cache\Lock[https://github.com/illuminate/contracts/blob/{{version}}/Cache/Lock.php] | |
Illuminate\Contracts\Cache\LockProviderIlluminate\Contracts\Cache\LockProvider[https://github.com/illuminate/contracts/blob/{{version}}/Cache/LockProvider.php] | |
Illuminate\Contracts\Cache\RepositoryIlluminate\Contracts\Cache\Repository[https://github.com/illuminate/contracts/blob/{{version}}/Cache/Repository.php] | Cache::driver() Cache::driver() |
Illuminate\Contracts\Cache\StoreIlluminate\Contracts\Cache\Store[https://github.com/illuminate/contracts/blob/{{version}}/Cache/Store.php] | |
Illuminate\Contracts\Config\RepositoryIlluminate\Contracts\Config\Repository[https://github.com/illuminate/contracts/blob/{{version}}/Config/Repository.php] | Config Config |
Illuminate\Contracts\Console\ApplicationIlluminate\Contracts\Console\Application[https://github.com/illuminate/contracts/blob/{{version}}/Console/Application.php] | |
Illuminate\Contracts\Console\KernelIlluminate\Contracts\Console\Kernel[https://github.com/illuminate/contracts/blob/{{version}}/Console/Kernel.php] | Artisan Artisan |
Illuminate\Contracts\Container\ContainerIlluminate\Contracts\Container\Container[https://github.com/illuminate/contracts/blob/{{version}}/Container/Container.php] | App App |
Illuminate\Contracts\Cookie\FactoryIlluminate\Contracts\Cookie\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Cookie/Factory.php] | Cookie Cookie |
Illuminate\Contracts\Cookie\QueueingFactoryIlluminate\Contracts\Cookie\QueueingFactory[https://github.com/illuminate/contracts/blob/{{version}}/Cookie/QueueingFactory.php] | Cookie::queue() Cookie::queue() |
Illuminate\Contracts\Database\ModelIdentifierIlluminate\Contracts\Database\ModelIdentifier[https://github.com/illuminate/contracts/blob/{{version}}/Database/ModelIdentifier.php] | |
Illuminate\Contracts\Debug\ExceptionHandlerIlluminate\Contracts\Debug\ExceptionHandler[https://github.com/illuminate/contracts/blob/{{version}}/Debug/ExceptionHandler.php] | |
Illuminate\Contracts\Encryption\EncrypterIlluminate\Contracts\Encryption\Encrypter[https://github.com/illuminate/contracts/blob/{{version}}/Encryption/Encrypter.php] | Crypt Crypt |
Illuminate\Contracts\Events\DispatcherIlluminate\Contracts\Events\Dispatcher[https://github.com/illuminate/contracts/blob/{{version}}/Events/Dispatcher.php] | Event Event |
Illuminate\Contracts\Filesystem\CloudIlluminate\Contracts\Filesystem\Cloud[https://github.com/illuminate/contracts/blob/{{version}}/Filesystem/Cloud.php] | Storage::cloud() Storage::cloud() |
Illuminate\Contracts\Filesystem\FactoryIlluminate\Contracts\Filesystem\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Filesystem/Factory.php] | Storage Storage |
Illuminate\Contracts\Filesystem\FilesystemIlluminate\Contracts\Filesystem\Filesystem[https://github.com/illuminate/contracts/blob/{{version}}/Filesystem/Filesystem.php] | Storage::disk() Storage::disk() |
Illuminate\Contracts\Foundation\ApplicationIlluminate\Contracts\Foundation\Application[https://github.com/illuminate/contracts/blob/{{version}}/Foundation/Application.php] | App App |
Illuminate\Contracts\Hashing\HasherIlluminate\Contracts\Hashing\Hasher[https://github.com/illuminate/contracts/blob/{{version}}/Hashing/Hasher.php] | Hash Hash |
Illuminate\Contracts\Http\KernelIlluminate\Contracts\Http\Kernel[https://github.com/illuminate/contracts/blob/{{version}}/Http/Kernel.php] | |
Illuminate\Contracts\Mail\MailQueueIlluminate\Contracts\Mail\MailQueue[https://github.com/illuminate/contracts/blob/{{version}}/Mail/MailQueue.php] | Mail::queue() Mail::queue() |
Illuminate\Contracts\Mail\MailableIlluminate\Contracts\Mail\Mailable[https://github.com/illuminate/contracts/blob/{{version}}/Mail/Mailable.php] | |
Illuminate\Contracts\Mail\MailerIlluminate\Contracts\Mail\Mailer[https://github.com/illuminate/contracts/blob/{{version}}/Mail/Mailer.php] | Mail Mail |
Illuminate\Contracts\Notifications\DispatcherIlluminate\Contracts\Notifications\Dispatcher[https://github.com/illuminate/contracts/blob/{{version}}/Notifications/Dispatcher.php] | Notification Notification |
Illuminate\Contracts\Notifications\FactoryIlluminate\Contracts\Notifications\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Notifications/Factory.php] | Notification Notification |
Illuminate\Contracts\Pagination\LengthAwarePaginatorIlluminate\Contracts\Pagination\LengthAwarePaginator[https://github.com/illuminate/contracts/blob/{{version}}/Pagination/LengthAwarePaginator.php] | |
Illuminate\Contracts\Pagination\PaginatorIlluminate\Contracts\Pagination\Paginator[https://github.com/illuminate/contracts/blob/{{version}}/Pagination/Paginator.php] | |
Illuminate\Contracts\Pipeline\HubIlluminate\Contracts\Pipeline\Hub[https://github.com/illuminate/contracts/blob/{{version}}/Pipeline/Hub.php] | |
Illuminate\Contracts\Pipeline\PipelineIlluminate\Contracts\Pipeline\Pipeline[https://github.com/illuminate/contracts/blob/{{version}}/Pipeline/Pipeline.php] | |
Illuminate\Contracts\Queue\EntityResolverIlluminate\Contracts\Queue\EntityResolver[https://github.com/illuminate/contracts/blob/{{version}}/Queue/EntityResolver.php] | |
Illuminate\Contracts\Queue\FactoryIlluminate\Contracts\Queue\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Queue/Factory.php] | Queue Queue |
Illuminate\Contracts\Queue\JobIlluminate\Contracts\Queue\Job[https://github.com/illuminate/contracts/blob/{{version}}/Queue/Job.php] | |
Illuminate\Contracts\Queue\MonitorIlluminate\Contracts\Queue\Monitor[https://github.com/illuminate/contracts/blob/{{version}}/Queue/Monitor.php] | Queue Queue |
Illuminate\Contracts\Queue\QueueIlluminate\Contracts\Queue\Queue[https://github.com/illuminate/contracts/blob/{{version}}/Queue/Queue.php] | Queue::connection() Queue::connection() |
Illuminate\Contracts\Queue\QueueableCollectionIlluminate\Contracts\Queue\QueueableCollection[https://github.com/illuminate/contracts/blob/{{version}}/Queue/QueueableCollection.php] | |
Illuminate\Contracts\Queue\QueueableEntityIlluminate\Contracts\Queue\QueueableEntity[https://github.com/illuminate/contracts/blob/{{version}}/Queue/QueueableEntity.php] | |
Illuminate\Contracts\Queue\ShouldQueueIlluminate\Contracts\Queue\ShouldQueue[https://github.com/illuminate/contracts/blob/{{version}}/Queue/ShouldQueue.php] | |
Illuminate\Contracts\Redis\FactoryIlluminate\Contracts\Redis\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Redis/Factory.php] | Redis Redis |
Illuminate\Contracts\Routing\BindingRegistrarIlluminate\Contracts\Routing\BindingRegistrar[https://github.com/illuminate/contracts/blob/{{version}}/Routing/BindingRegistrar.php] | Route Route |
Illuminate\Contracts\Routing\RegistrarIlluminate\Contracts\Routing\Registrar[https://github.com/illuminate/contracts/blob/{{version}}/Routing/Registrar.php] | Route Route |
Illuminate\Contracts\Routing\ResponseFactoryIlluminate\Contracts\Routing\ResponseFactory[https://github.com/illuminate/contracts/blob/{{version}}/Routing/ResponseFactory.php] | Response Response |
Illuminate\Contracts\Routing\UrlGeneratorIlluminate\Contracts\Routing\UrlGenerator[https://github.com/illuminate/contracts/blob/{{version}}/Routing/UrlGenerator.php] | URL URL |
Illuminate\Contracts\Routing\UrlRoutableIlluminate\Contracts\Routing\UrlRoutable[https://github.com/illuminate/contracts/blob/{{version}}/Routing/UrlRoutable.php] | |
Illuminate\Contracts\Session\SessionIlluminate\Contracts\Session\Session[https://github.com/illuminate/contracts/blob/{{version}}/Session/Session.php] | Session::driver() Session::driver() |
Illuminate\Contracts\Support\ArrayableIlluminate\Contracts\Support\Arrayable[https://github.com/illuminate/contracts/blob/{{version}}/Support/Arrayable.php] | |
Illuminate\Contracts\Support\HtmlableIlluminate\Contracts\Support\Htmlable[https://github.com/illuminate/contracts/blob/{{version}}/Support/Htmlable.php] | |
Illuminate\Contracts\Support\JsonableIlluminate\Contracts\Support\Jsonable[https://github.com/illuminate/contracts/blob/{{version}}/Support/Jsonable.php] | |
Illuminate\Contracts\Support\MessageBagIlluminate\Contracts\Support\MessageBag[https://github.com/illuminate/contracts/blob/{{version}}/Support/MessageBag.php] | |
Illuminate\Contracts\Support\MessageProviderIlluminate\Contracts\Support\MessageProvider[https://github.com/illuminate/contracts/blob/{{version}}/Support/MessageProvider.php] | |
Illuminate\Contracts\Support\RenderableIlluminate\Contracts\Support\Renderable[https://github.com/illuminate/contracts/blob/{{version}}/Support/Renderable.php] | |
Illuminate\Contracts\Support\ResponsableIlluminate\Contracts\Support\Responsable[https://github.com/illuminate/contracts/blob/{{version}}/Support/Responsable.php] | |
Illuminate\Contracts\Translation\LoaderIlluminate\Contracts\Translation\Loader[https://github.com/illuminate/contracts/blob/{{version}}/Translation/Loader.php] | |
Illuminate\Contracts\Translation\TranslatorIlluminate\Contracts\Translation\Translator[https://github.com/illuminate/contracts/blob/{{version}}/Translation/Translator.php] | Lang Lang |
Illuminate\Contracts\Validation\FactoryIlluminate\Contracts\Validation\Factory[https://github.com/illuminate/contracts/blob/{{version}}/Validation/Factory.php] | Validator Validator |
Illuminate\Contracts\Validation\ImplicitRuleIlluminate\Contracts\Validation\ImplicitRule[https://github.com/illuminate/contracts/blob/{{version}}/Validation/ImplicitRule.php] | |
Illuminate\Contracts\Validation\RuleIlluminate\Contracts\Validation\Rule[https://github.com/illuminate/contracts/blob/{{version}}/Validation/Rule.php] | |
Illuminate\Contracts\Validation\ValidatesWhenResolvedIlluminate\Contracts\Validation\ValidatesWhenResolved[https://github.com/illuminate/contracts/blob/{{version}}/Validation/ValidatesWhenResolved.php] | |
Illuminate\Contracts\Validation\ValidatorIlluminate\Contracts\Validation\Validator[https://github.com/illuminate/contracts/blob/{{version}}/Validation/Validator.php] | Validator::make() Validator::make() |
Illuminate\Contracts\View\EngineIlluminate\Contracts\View\Engine[https://github.com/illuminate/contracts/blob/{{version}}/View/Engine.php] | |
Illuminate\Contracts\View\FactoryIlluminate\Contracts\View\Factory[https://github.com/illuminate/contracts/blob/{{version}}/View/Factory.php] | View View |
Illuminate\Contracts\View\ViewIlluminate\Contracts\View\View[https://github.com/illuminate/contracts/blob/{{version}}/View/View.php] | View::make() View::make() |