Laravel Redirect does not work in Event handler / listener

后端 未结 2 594
旧时难觅i
旧时难觅i 2021-01-19 18:35

I have a Auth.Attempt event handler class, which I detect user\'s login attempts to decide to lock user\'s account. However, when I tried to redirect user to login page with

相关标签:
2条回答
  • 2021-01-19 19:21

    Not getting to much into this very interesting discussion:

    Should exceptions be used for flow control

    You can try setting up your own exception handler and redirect from there on to the login page.

    class FancyException extends Exception {}
    
    App::error(function(FancyException $e, $code, $fromConsole)
    {
        $msg = $e->getMessage();        
        Log::error($msg);
    
        if ( $fromConsole )
        {
            return 'Error '.$code.': '.$msg."\n";
        }
    
        if (Config::get('app.debug') == false) {
            return Redirect::route('your.login.route');
        }
        else
        {
            //some debug stuff here
        }
    
    
    });
    

    And in your function:

    public function onLoginAttempt($data)
    {
        //detection process.......
        // if login attempts more than max attempts
        throw new FancyException("some msg here");
    }
    
    0 讨论(0)
  • 2021-01-19 19:23

    You can still send an HttpException who will work. But obviously instructions after the event handler will not be interpreted

    abort(redirect('/'));
    
    0 讨论(0)
提交回复
热议问题