Laravel存储库模型中的API调用


API Calls in Laravel Repository Model

我正在开发一个web应用程序,在该应用程序中我将使用第三方API集成(支付网关、SMS供应商、mailchimp等电子邮件服务和其他外部API)。我的应用程序将具有存储库模式,该模式将具有与我的每个模型相关联的存储库,并且我的控制器将使用存储库功能。

现在我的所有API集成都在哪里?我是否应该将所有API集成保存在一个存储库中?

示例:

如果我有一个短信提供商Valuefirst,我的Laravel存储库设计是什么样子的?

我的短信界面

interface ISmsProvider {
    public function sendSms($mobileNo,$msg);
}

我的ValueFirst(SMS提供商)存储库

class ValueFirstRepository implements ISmsProvider {
    public function sendSMS($mobieNo,$msg)
    {
       //API Call to ValueFirst SMS provider
       // Any database insertions/updates to SMS model
       // Any file logs
    }
}

假设我有一个服务提供者类,在那里我完成了接口和存储库的绑定,我的控制器将看起来像

SMS控制器

class SmsController extends BaseController {
    // ISmsProvider is the interface
    public function __construct(ISmsProvider $sms)
    {
        $this->sms = $sms;
    }
    public function sendSMS()
    {
        $mobileNo = '9999999999';
        $msg = 'Hello World!';
        $users = $this->sms->sendSMS($mobileNo,$msg); 
    }    
}

我的问题

  1. 这是使用存储库模式进行API集成的正确方法吗
  2. ValueFirst存储库中是否发生任何与数据库活动相关的事件?例如:如果我想在ValueFirst的API响应之后更新记录,我会在存储库中进行吗
  3. 如果是对的,有人能告诉我模型交互是如何从存储库中进行的吗
  4. 现在,如果我需要在ValueFirst或任何其他供应商之间做出选择,该怎么办?我怎样才能从控制器上完成它

我将使用这个项目作为我的应用程序的框架

l5储存库

请帮我设计一下,我对Laravel/repository模式还很陌生。努力学习。TIA:)

你的方法对我来说似乎很好,但我需要改进

首先,我将保留ValueFirstRepository类,该类只负责管理SMS API,并注入特定的存储库类(SmsRepository),通过雄辩(或者如果不需要存储库,则仅为模型)与DB交互

这里重要的一点是保持在一个类中管理API的责任性和在另一个类()中与DB交互的责任性

ValueFirstRepository

class ValueFirstRepository implements ISmsProvider 
{
    //INJECT REPOSITORY CLASS TO INTERACT WITH DB
    public function __construct(SmsRepository $smsRepo )
    {
        $this->smsRepo = $smsRepo;    
    }
    //this method will be called from your controller
    public function sendSMS($mobieNo,$msg)
    {
       //API CALL
        $info = this->sendSMSAPI($mobieNo,$msg);
       //DB CALL
        $this->smsRepo->save( $info );
       // Any file logs
    }
    //this will actually interact with the API
    protected function sendSMSAPI($mobieNo,$msg)
    {
       //API Call to ValueFirst SMS provider
    }
}

该解决方案的litte变体可以是使用事件,并在发送短信时在ValueFirstRepository类中激发事件,并响应该事件实现一些侦听器,这些侦听器将执行与事件相关的其他操作

另一种替代解决方案可以是直接在控制器中处理步骤:

SMS控制器

//INJECT THE DEPENDECIES IN YOUR CONTROLLER
public function __construct(ISmsProvider $sms, SmsRepository $smsRepo )
{
    $this->sms = $sms;
    $this->smsRepo = $smsRepo;
}
public function sendSMS()
{
    //send SMS
    $mobileNo = '9999999999';
    $msg = 'Hello World!';
    $info= $this->sms->sendSMS($mobileNo,$msg);
    //use the model to save data
    this->$smsRepo->save($info); 
} 

这一次,SmsRepository的依赖项将被注入控制器中,而不是ValueFirstRepository类,控制器的方法会更大一点,但的最佳方法由您决定

对于最后一个问题:如果您想更改供应商提供商,您可以使用Laravel的功能通过bind方法将接口绑定到实现:

App::bind( App'ISmsProvider::class , App'ValueFirstRepository::class );

这将告诉laravel-wich类在请求特定接口时注入。因此,在这种情况下,当请求ISmsProvider接口时,Laravel将自动注入ValueFirstRepository具体实例。

如果你想更改供应商,你只应该将行更改为:

App::bind( App'ISmsProvider::class , App'AnotherSMSVendorRepository::class ); 

并且将注入CCD_ 9类而不是CCD_