API 在 Laravel 存储库模型中调用
API Calls in Laravel Repository Model
我正在开发一个 Web 应用程序,我将在其中使用第三方 API 集成(支付网关、SMS 供应商、mailchimp 等电子邮件服务、其他外部 APIs)。我的应用程序将具有存储库模式,该模式将具有与我的每个模型关联的存储库,并且我的控制器将使用存储库功能。
现在我所有的 API 集成从哪里来?我应该将所有 API 集成保存在一个存储库中吗?
示例:
如果我有 SMS 提供商 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
}
}
假设我有一个服务提供商class,我已经完成了接口和存储库的绑定,我的控制器看起来像
短信控制器
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);
}
}
我的问题
- 这是使用存储库模式 API 集成的正确方法吗?
- ValueFirst 存储库中是否发生了与任何数据库活动相关的任何事情?例如:如果我想在 ValueFirst 的 API 响应后更新记录,我是否在存储库中进行?
- 如果正确,有人可以告诉我模型交互是如何从存储库发生的吗?
- 现在,如果我需要在 ValueFirst 或任何其他供应商之间做出选择,该怎么做?我怎样才能从控制器做到这一点?
我将使用此项目作为我的应用程序的框架
请帮助我进行此设计,我对 Laravel / 存储库模式还很陌生。努力学习东西。 TIA :)
你的方法对我来说似乎很好,但有些地方我会改进
首先,我会保留 ValueFirstRepository
class 的唯一职责是管理您的 SMS API,并注入特定的存储库 class(SmsRepository
) 通过 eloquent 与数据库交互(如果不需要存储库,则仅使用模型)
这里的重点是将管理您的 API 的职责放在一个 class 中,将与数据库交互的责任放在另一个中:
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
}
}
此解决方案的一个小变体可能是使用事件,并在发送短信时在 ValueFirstRepository
class 中触发事件,并响应该事件实施一些将执行其他操作的侦听器与事件相关
另一种替代解决方案是直接在您的控制器中处理这些步骤:
短信控制器
//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
class,并且控制器的方法会更大一点,但它是由您来决定最适合您的方式
最后一个问题:如果你想改变你的供应商提供者,你可以使用Laravel的能力通过bind
方法bind interfaces to implementation:
App::bind( App\ISmsProvider::class , App\ValueFirstRepository::class );
这将告诉 laravel 在请求特定接口时 class 注入。因此,在这种情况下,当请求 ISmsProvider
接口时,Laravel 将自动注入一个 ValueFirstRepository
具体实例。
如果您想更改供应商,您只需将行更改为:
App::bind( App\ISmsProvider::class , App\AnotherSMSVendorRepository::class );
并且 AnotherSMSVendorRepository
class 将被注入而不是 ValueFirstRepository
我正在开发一个 Web 应用程序,我将在其中使用第三方 API 集成(支付网关、SMS 供应商、mailchimp 等电子邮件服务、其他外部 APIs)。我的应用程序将具有存储库模式,该模式将具有与我的每个模型关联的存储库,并且我的控制器将使用存储库功能。
现在我所有的 API 集成从哪里来?我应该将所有 API 集成保存在一个存储库中吗?
示例:
如果我有 SMS 提供商 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
}
}
假设我有一个服务提供商class,我已经完成了接口和存储库的绑定,我的控制器看起来像
短信控制器
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);
}
}
我的问题
- 这是使用存储库模式 API 集成的正确方法吗?
- ValueFirst 存储库中是否发生了与任何数据库活动相关的任何事情?例如:如果我想在 ValueFirst 的 API 响应后更新记录,我是否在存储库中进行?
- 如果正确,有人可以告诉我模型交互是如何从存储库发生的吗?
- 现在,如果我需要在 ValueFirst 或任何其他供应商之间做出选择,该怎么做?我怎样才能从控制器做到这一点?
我将使用此项目作为我的应用程序的框架
请帮助我进行此设计,我对 Laravel / 存储库模式还很陌生。努力学习东西。 TIA :)
你的方法对我来说似乎很好,但有些地方我会改进
首先,我会保留 ValueFirstRepository
class 的唯一职责是管理您的 SMS API,并注入特定的存储库 class(SmsRepository
) 通过 eloquent 与数据库交互(如果不需要存储库,则仅使用模型)
这里的重点是将管理您的 API 的职责放在一个 class 中,将与数据库交互的责任放在另一个中:
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
}
}
此解决方案的一个小变体可能是使用事件,并在发送短信时在 ValueFirstRepository
class 中触发事件,并响应该事件实施一些将执行其他操作的侦听器与事件相关
另一种替代解决方案是直接在您的控制器中处理这些步骤:
短信控制器
//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
class,并且控制器的方法会更大一点,但它是由您来决定最适合您的方式
最后一个问题:如果你想改变你的供应商提供者,你可以使用Laravel的能力通过bind
方法bind interfaces to implementation:
App::bind( App\ISmsProvider::class , App\ValueFirstRepository::class );
这将告诉 laravel 在请求特定接口时 class 注入。因此,在这种情况下,当请求 ISmsProvider
接口时,Laravel 将自动注入一个 ValueFirstRepository
具体实例。
如果您想更改供应商,您只需将行更改为:
App::bind( App\ISmsProvider::class , App\AnotherSMSVendorRepository::class );
并且 AnotherSMSVendorRepository
class 将被注入而不是 ValueFirstRepository