首页 文章

Laravel 5.3 - 将'guest redirect'更改为'index'而不是'login' route

提问于
浏览
0

好吧,基本的问题是Laravel 5.3将访客用户重定向到'/ login'路由,我在'app / Exceptions / Handler.php'中将其更改为我的索引'/',因为我没有登录页面/登录是位于指数 .

之前:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    // IMPORTANT LINE
    return redirect()->guest('/login');
}

后:

...
return redirect()->guest('/');
...

在此更改之后,guest用户被无限循环地重定向到索引文件,因为他在访问'/'路由时没有登录,因此再次被重定向到'/' .

我认为解决方案很简单,我会在重定向之前检查文件中的当前url:

if ($_SERVER['REQUEST_URI']) !== '/')
{
    return redirect ('/');
}

这没用,所以我尝试了laravel方式:

if (!$request()->is('/'))
{
    return redirect ('/');
}

相同的行为/例外:

ErrorException in VerifyCsrfToken.php line 136:
Trying to get property of non-object

1 回答

  • 1

    我假设你的索引路由加载的东西就像你的主页,所以你需要删除你的主页控制器中的auth中间件,或者应用程序将被卡在无限循环中,就像它是正确的一样现在 . 为了坚持您的改变,您只需在路由控制器中评论auth中间件 .

    public function __construct()
      {
        // $this->middleware('auth');
      }
    

相关问题