// 一般ユーザー認証サービス
$this->app->when(UserAuthService::class)
->needs(AuthServiceInterface::class)
->give(function () {
return new BasicAuthService();
});
// 管理者認証サービス
$this->app->when(AdminAuthService::class)
->needs(AuthServiceInterface::class)
->give(function () {
return new TwoFactorAuthService();
});
AuthServiceInterface
という同じインターフェースに対して、ユーザー種別に応じて異なる認証サービスを注入しています:UserAuthService
にはBasicAuthService
を注入
AdminAuthService
にはTwoFactorAuthService
を注入