2017-10-05 10 views
0

人。私はララヴェルの新人です。ただ、5.5をインストールし、問題が(含むAuthenticationExceptionのinstanceof $例外)常にfalseを返している

public function render($request, Exception $exception) 
{ 
    if ($exception instanceof AuthenticationException) { 
     //Do something 
    } 
} 

の下のようなハンドラ \ のApp \例外に含むAuthenticationExceptionをキャッチしてみてください。

dd($exception instanceof AuthenticationException) //return false. 

私は($例外を)ddは、私はその後、私は助けてください

dd($exception instanceof Exception) //return true. 

、しかし

get_class($exception) return \Illuminate\Auth\AuthenticationException 

を試してみてください

AuthenticationException{ 
    #gurad... 
    .... 
    ..... 
} 

を得ました。ありがとう。あなたが言及した

public function render($request, Exception $exception) 
{ 
    if ($exception instanceof \Illuminate\Auth\AuthenticationException) { 
     //Do something 
    } 

    return parent::render($request, $exception); 
} 

+0

私の答え[ここ](https://stackoverflow.com/a/44950318/4881811)を参照してください:) – Maraboc

+0

"use \ Illuminate \ Auth \ AuthenticationException;"という行を含める必要があります。あなたのHandler.phpでそれ以外の場合、常にfalseを返します。 –

答えて

0

あなたは必ず有効な名前空間のクラスを使用するべきである

DD(例外のinstanceof $例外)がtrueを返す//。

これは本当です。

常に //Do something 1
public function render($request, Exception $exception) 
{ 
    if ($exception instanceof Exception) { 
     //Do something 1 
    } 
    if ($exception instanceof \Illuminate\Auth\AuthenticationException) { 
     //Do something 2 
    } 

    return parent::render($request, $exception); 
} 

は次のようになります。このためにtrueを返しますExceptionクラスを拡張します。各例外クラスは、それがなぜあなたのハンドラで、あなたが使用する場合は、最初の例のために、特定のクラスではなく例外クラスを確認することを確認する必要があります最初に打ち上げられた。

関連する問題