0

当用户未登录时,我在将内置 Laravel Auth 重定向到正确路径时遇到问题。

从文档看来,我可以将以下内容添加到 AuthController.php

    // redirect paths
    protected $redirectPath        = '/profile';
    protected $loginPath           = '/home';
    protected $redirectAfterLogout = '/home';

添加它应该允许我控制在各种情况下重定向用户的位置,但似乎没有任何效果。

挖掘代码我可以看到 auth/login 路由是在 handle 函数的 middleware/authenticate.php 中设置的,如下所示:

    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('auth/login');
            }
        }

        return $next($request);
    }

如果我在这里更改路径,它会起作用,但这肯定不是实现它的最佳方法。

我正在使用 Laravel 5.1.7 并尝试过 php artisan route:clear

任何想法/建议都会很棒。

4

1 回答 1

0

在您的控制器中使用您想要对已注销用户隐藏的代码块。此代码会将任何已注销的用户重定向到$loginPath,您也可以在登录后使用它protected $redirectTo = 'yourDesirepath'来重定向用户。

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

或者,如果您愿意,您只能保护几种方法。在您的方法中添加这行代码。

$this->middleware('auth');
于 2015-11-01T04:18:50.717 回答