【问题标题】:PHP (Laravel) Dependency Injection BasicsPHP (Laravel) 依赖注入基础
【发布时间】:2017-12-07 21:37:17
【问题描述】:

我最近遇到了 SOLID 设计原则,并一直在寻找将依赖注入合并到我的工作中的方法,以便我可以编写更好的测试并模拟任何外部调用。我在 PHP Laravel (Lumen) 中尝试了一些依赖注入,但下面的内容似乎不对。

创建依赖项的代码是在其中一个控制器的公共方法中创建的。这似乎不是放置它的合适位置,因为我现在有很多非常丑陋的代码在控制器内部实例化对象,而不仅仅是接收请求并打包响应。是否有更适合进行实例化的地方?也适用于某些物品,例如$paying_user object。我正在创建一个空的 Eloquent 模型,使用一种方法来检索集合中的第一项并将其与返回的对象交换以进行注入。这也感觉不对。谁能指出构建以下内容的最佳和正确方法应该是什么?

public function cancel_subscription(Request $request)
{
    $result = false;

    // Create dependencies
    $platform_id = Sanitiser::clean_integer($request->input('platform_id'));
    $paying_user = new PayingUserModel();
    $paying_user = $paying_user->where('platform_id','=',$platform_id)->first();
    $current_subscription_model = $paying_user->non_cancelled_subscriptions->first();
    $refund_model = new RefundModel();
    $stripe = new Stripe();
    $stripe_refund = new Refund();
    $charge = new ChargeModel();
    $stripe::setAPIkey(env('STRIPE_SK'));
    $stripe_subscription = new Subscription();

    // Create services and inject
    $this->cancellation_service = new CancellationService($paying_user,$current_subscription_model,$stripe_subscription);
    $this->refund_service = new RefundService($paying_user,$current_subscription_model,$refund_model,$stripe_refund,$charge);

    // Run services
    $result = $this->cancellation_service->cancel_subscription();
    if($this->cancellation_service->get_refund_required()==true){
        $result = $this->refund_service->handle_refund();
    }
}

【问题讨论】:

  • "它下面的 $paying_user 对象在被注入之前与实际的付费用户对象交换" ...什么?
  • $paying_user = new PayingUserModel(); $paying_user = $paying_user->where('platform_id','=',$platform_id)->first() //试图说我正在创建一个空的 Eloquent 模型,用它来检索集合的第一项(获取实际数据)用于我想注入其他类的模型。
  • “...在被注入之前”部分...当您自己调用方法或实例化对象时,不会发生“注入”
  • 你在Controller的一个方法里做的太多了,首先你应该把关注点分开不同类型的对象/不同的地方。
  • 如果我拆分方法并将退款活动移动到一个新方法,控制器是否仍然是实例化这些空对象并注入其他对象的最合适位置?

标签: php laravel dependency-injection


【解决方案1】:

如果我理解得很好,您正在尝试使用依赖注入来组织您的代码。我认为最简单的方法是使用 Laravel 的自动注入。

查看您的代码,定义cancel_subscription() 的类取决于CancellationServiceRefundService

同时CancellationService 依赖于PayingUserModelSubscription。而RefundService 取决于PayingUserModelRefundModelRefundChargeModel

您可以通过类型提示每个类构造函数中的参数来使用自动注入。

例如CancellationService 类构造函数看起来像:

class CancellationService
{
    protected $payingUserModel;

    protected $subscription;

    public function __construct(PayingUserModel $payingUserModel, Subscription $subscription)
    {
        $this->payingUserModel = $payingUserModel;

        $this->subscription = $subscription;
    }
}

现在,对于PayingUserModel,您要传递该类的特定实例。

这可以通过将实例绑定到AppServiceProvider 类中的容器来完成。

$platform_id = request()->input('platform_id');

$user = PayingUserModel::where('platform_id', '=', $platform_id)->first();

$this->app->instance('PayingUserModel', $user);

或者,您可以将 PayingUserModel 实例注册为单例。

$this->app->singleton('PayingUserModel', function() use($platform_id) {
    return PayingUserModel::where('platform_id', '=', $platform_id)->first();
});

我自己没有尝试过,所以我不能 100% 确定这是否是正确的方法。但根据Laravel documentation,这是您管理依赖注入的方式,希望它能让您了解如何进行。

【讨论】:

  • 感谢卡米洛,我已经设法实现了非常接近建议的东西。
  • 很高兴能帮上忙!我花了一些时间来理解问题并提出建议。