2016-10-24 3 views
0

TFA라는 인터페이스와 GoogleTFA라는 구현이 있습니다. 이건 내 방법입니다사용자 모델에서 바인드가 작동하지 않습니다.

Type error: Argument 1 passed to App\Models\User::toggleTFA() must implement interface App\Contracts\TFA, none given

:하지만 내 사용자 모델에 TFA를 사용하려고 때마다 나는이 오류 얻을

public function toggleTFA(TFA $tfa) 
    { 
     /** 
     * If we're disabling TFA then we reset his secret key. 
     */ 
     if ($this->tfa === true) 
      $this->tfa_secret_key = $tfa->getSecretKey(); 

     $this->tfa = !$this->tfa; 
     $this->save(); 
    } 

을하고이 AppServiceProvider.php 내 바인드입니다 :

public function register() 
    { 
     /** 
     * GoogleTFA as default TFA adapter. 
     */ 
     $this->app->bind('App\Contracts\TFA', 'App\Models\GoogleTFA'); 
    } 

왜 이런 동작이 발생하는지 알고 싶습니다. TFA $ tfa 힌트를 내 컨트롤러의 모든 메서드에 입력하면 작동하지만 모델에 논리를 유지하려고합니다. 미리 감사드립니다.

+0

** 전화하는 방법과 같은 정보를 제공해주십시오 ** toggleTFA ** – Haridarshan

답변

1

DI은 모든 방법에서 작동하지 않습니다. 컨트롤러 메소드는 Laravel을 사용하여이를 사용합니다.

한 가지 방법은 수동으로 해결하는 것입니다 모델의 작업 내용 :

$tfa = app(TFA::class); 

당신은 몇 가지 다른 방법이를 사용하고 있다면 나는 그것을 자신의 방법으로 위를 이동한다.

파일 app/Facades/Tfa.php을 만들고 여기에 다음을 추가

또는, (아래 예제는 당신이 당신 App 네임 스페이스에 정면 배치됩니다 가정한다) 당신의 TFA 구현을 위해 특별히 Facade를 만들 수 있습니다 :

에서 다음
<?php 

namespace App\Facades; 

use Illuminate\Support\Facades\Facade; 

class Tfa extends Facade 
{ 
    /** 
    * Get the registered name of the component. 
    * 
    * @return string 
    */ 
    protected static function getFacadeAccessor() 
    { 
     return 'App\Contracts\TFA'; 
    } 

} 

당신의 config/app.php 하단에하여 aliases 배열에 다음을 추가

012 당신은 단지 외관에서 getSecretKey를 호출 할 수 있습니다

이 방법이 도움이

public function toggleTFA() 
{ 
    /** 
    * If we're disabling TFA then we reset his secret key. 
    */ 
    if ($this->tfa === true) 
     $this->tfa_secret_key = Tfa::getSecretKey(); 

    $this->tfa = !$this->tfa; 
    $this->save(); 
} 

희망!

+0

더 좋은 해결책이 없습니까? – Martin

+0

@Martin 정의가 더 좋습니다 –

+0

가장 깨끗한 코드를 얻으려고합니다. 다른 해결책이 있습니까? 모델에서 DI를 사용할 수 있습니까? – Martin

관련 문제