2017-11-14 8 views
0

私はlaravelで同じsoapクライアントを使用する別の関数を持っています: これらの関数のsoapclientの重複したコードは間違っています。 私はtraitファイルを試しましたが、私はそのように動作するようには見えません。再利用可能なコードsoapクライアントlaravel

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); 
    } 
+0

は、彼らが同じクラスにいますか?その場合は、SOAPを作成して返す関数を作成するか、SOAPを作成して呼び出すSOAPメソッドを呼び出す関数を作成します。同じクラスにない場合は、SOAPの新しいクラスを作成してSOAPを作成し、メソッドを呼び出します。 – aynber

+0

リポジトリパターンはどうですか? –

答えて

0

新しいクラスを作成します。

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; 
    } 
} 
+0

ありがとうございました –

関連する問題