如果在 Laravel 5.3 中通过身份验证则重定向

Redirect if authenticated in Laravel 5.3

我在 Laravel 5.3 中使用 Auth 脚手架,我已经更改了 auth 的路由。因此,我使用 /signin/signup.

而不是 /login/register

Laravel 5.2 中,我们默认在 auth 中间件中有这个,

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        }

        return redirect()->guest('login');
    }

    return $next($request);
}

如果用户没有登录,这将重定向到login路由。在Laravel 5.3中我们有这个,

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
}

这会将已登录的用户重定向到默认路由 /。所以他们在 5.3 中进行了调换。我们没有定义访客的去向,而是定义了登录用户的去向。

我的问题是,我如何本地到Laravel 5.3改变客人去?

因为目前,尝试访问受中间件保护的站点的人会自动以 /login 路径结束。我想将其更改为 /signin,但我找不到任何地方来自定义此行为。

有什么想法吗?

how would I "natively" to Laravel 5.3 change were guests go?

Looks like app/Exceptions/Handler.php 中有一个新的 unauthenticated() 方法,它处理未经身份验证的用户并重定向到 login

因为这是您应用程序的一部分,您没有理由不能自定义它以重定向到其他地方。

你可以在 app.blade.php 中用 JS 试试这个:

@if (Auth::guest())
<script type="text/javascript"> window.location = "{{url('/login')}}";    </script>
@endif