防止注销auth laravel

时间:2016-06-24 08:08:40

标签: php laravel

有没有办法监听注销事件并采取重新定位auth laravel等决策?我知道有一些登录/注销侦听器,但重定向不起作用:

class LogSuccessfulLogout
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Logout  $event
     * @return void
     */
   public function handle(Logout $event)
   {
    if($event->user) {


        $new = Auth::user()->cars()->where('status', 1)->count();
        $inProgress = Auth::user()->cars()->where('status', 2)->count();

        if($new > 0 || $inProgress > 0){
            redirect('/');
        }
    }

  }
}

1 个答案:

答案 0 :(得分:2)

这是默认的Laravel 5.2 AuthController

中调用的默认注销函数
public function logout()
{
    Auth::guard($this->getGuard())->logout();
    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

因此,您可以设置redirectAfterLogout属性以使用AuthController中的以下代码更改重定向网址,

private $redirectAfterLogout = '/new-logout-redirect';

或者您可以选择简单地覆盖注销功能。 重定向不会直接从您的监听器中运行。

这是修改后的logout函数和您的逻辑,您可以将它放在AuthController中,

public function logout()
{
    Auth::guard($this->getGuard())->logout();

    $new = Auth::user()->cars()->where('status', 1)->count();
    $inProgress = Auth::user()->cars()->where('status', 2)->count();

    if($new > 0 || $inProgress > 0){
        return redirect('/');
    }

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}
相关问题