从控制器laravel引用接口

时间:2017-09-21 14:35:06

标签: php laravel interface

我正在使用界面来处理不同的付款方式。

事情是在我的控制器中我得到了银行slug但是根据那个slug我决定在我的案例中哪个类银行用来处理付款。我的控制器:

public function sendPayment($request, PayBank $bank_transacton)
{

    here I want to reference the SentToBank interface and
   send payment class that I get in return from checkBankImplementation function//

    SendToBank()->sendLoanApplication($bank_transaction);
}

检查slug的方法,以便我知道要发送到接口的哪个支付服务:

public static function checkBankImplementation($bank_slug) {

    switch ($bank_slug) {

    case "firstbank":
        return new app\FirstBank\Client();
        break;

    case "secondbank":
        return new app\SecondBank\Client();
        break;
    default:

        return null;
    }
}

我的界面:

<?php

namespace App\Banks;

interface SendToBank {

    public function sendLoanApplication($bank_transaction);

}

1 个答案:

答案 0 :(得分:1)

1-您的app\FirstBank\Client()app\SecondBank\Client()需要实施SendToBank界面。

2-在控制器中,您必须使用静态方法创建正确的对象。

3-在该对象上调用sendLoanApplication方法

类似的东西:

public function sendPayment($request, PayBank $bank_transacton)
{

    $bankClient = Class::checkBankImplementation($bank_slug); // You need to change the class name with the one actually implementing the checkBandImplementation method and extract the proper $bank_slug from the request

    $bankClient->sendLoanApplication($bank_transaction);
}