【问题标题】:Laravel 5 Redirect After Logout - How to Redirect Back?Laravel 5 注销后重定向 - 如何重定向回来?
【发布时间】:2015-10-19 05:35:19
【问题描述】:

我想在用户成功注销后重定向回用户所在的位置,因为我有即使注销也可以访问的方法。

我保护我的 PhotosController 中的所有方法,除了 @show

public function __construct()
{
    $this->middleware('auth', ['except' => 'show']);
}

要在注销后设置重定向,我在 AuthController 中设置了如下属性:

protected $redirectAfterLogout = '/customLogoutPage';

但我想将用户重定向回他曾经去过的地方,因为即使没有被锁定,他也可以看到视图。

我朝这个方向尝试了一些东西:

protected $redirectAfterLogout = redirect()->back();

但我的浏览器显示:“意外的 '(', 期待 ',' 或 ';'

如何使用重定向回到用户退出之前所在的视图。

【问题讨论】:

  • 您无法让内置的 AuthController 执行此操作。您必须制作自己的逻辑版本。
  • 构建你的控制器,它可以做到这一点......
  • :/ 好的,谢谢。你碰巧有这方面的介绍吗?

标签: php laravel redirect


【解决方案1】:

内置的注销方法只接受一个字符串,你正在向它传递一个函数。如果你想要这种行为,你必须在你的AuthController 中实现你自己的注销方法。

幸运的是,这很简单:

public function getLogout()
{
    Auth::logout();

    return redirect()->back();
}

就是这样。

作为参考,这是 Laravel 使用的原始函数AuthenticatesUser trait:

/**
 * Log the user out of the application.
 *
 * @return \Illuminate\Http\Response
 */
public function getLogout()
{
    Auth::logout();

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

【讨论】:

    【解决方案2】:
    public function getLogout()
    {
        Auth::logout();
    
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/customLogoutPage');
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2018-01-23
      • 2012-11-05
      • 2018-07-21
      • 2015-12-18
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多