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 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持