【问题标题】:Reusable code soap client laravel可重用的代码肥皂客户端 laravel
【发布时间】:2018-04-27 15:41:28
【问题描述】:

我有不同的函数在 laravel 中使用相同的肥皂客户端: 这些函数中soapclient 的重复代码似乎是错误的。 我尝试了一个特征文件,但我似乎没有让它以这种方式工作。

 public function getStaff()
    {
        // INITIALISEER DE SOAP CLIENT
        $webservicesPwd = "apipwd";      
        $soap = new SoapClient('https://apiserver.com');

        // HAAL STUDENTEN OP VIA API CALL
        $result = $soap->apiFunction($webservicesPwd,2,1);

        // INDIEN RESULTAAT NIET CORRECT
        if(is_int($result)) {

           throw new \Exception($errorMessage);
        }

public function getStudents()
    {
          // INITIALISEER DE SOAP CLIENT
        $webservicesPwd = "apipwd";      
        $soap = new SoapClient('https://apiserver.com');

        // HAAL STUDENTEN OP VIA API CALL     
        $result = $this->soap-someOtherApiFunction($this->webservicesPwd,3,1);

        // INDIEN RESULTAAT NIET CORRECT
      if(is_int($result)) {

           throw new \Exception($errorMessage);
        }


        dd($result);
    }

【问题讨论】:

  • 他们是同一班吗?如果是这样,那么您可以创建一个创建soap 并返回它的函数,或者创建一个既创建soap 又调用您尝试调用的SOAP 方法的函数。如果不在同一个类中,那么您可以为创建肥皂并调用方法的 SOAP 创建一个新类。
  • 存储库模式怎么样?

标签: php laravel soap traits reusability


【解决方案1】:

创建一个新类:

class ApiNameClient {
    protected $soapClient;

    protected $webservicesPwd;

    public function __construct() {
    // initialize $soapClient and $webservicesPwd here
    }

    public function getStudents() {
        $result = $this->soapClient->myFirstAction($this->webservicesPwd,3,1);

        return $this->defendInteger($result);
    }

    protected function defendInteger($value) {
        if(is_int($value)) {
            throw new \Exception($errorMessage);
        }
        return $value;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多