dongyuling0312 2017-10-05 15:47
浏览 678
已采纳

Laravel 5.5 $异常实例AuthenticationException未按预期工作

guys. I am new to Laravel. Just installed 5.5 and try to catch the AuthenticationException in App\Exceptions\Handler like below

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

The problem is ($exception instanceof AuthenticationException) always return false.

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

When I dd($exception) I got

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

Then I try

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

However,

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

Please help. Thanks.

  • 写回答

1条回答 默认 最新

  • duanhuang3074 2017-10-05 18:41
    关注

    You should make sure you use class from valid namespace:

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

    You mentioned:

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

    That's true. Each exception class that will extend Exception class will return true for this, that's why in your handler you should make sure you first verify specific classes and not exception class, for example if you used:

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

    always //Do something 1 would be launched first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料