【问题标题】:Laravel Authenticate middleware differences between5.x and 7.xLaravel 5.x 和 7.x 认证中间件的区别
【发布时间】:2020-11-14 15:03:31
【问题描述】:

我正在升级使用 Laravel 5.3 编写的应用程序。现有代码有:

class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest((Request::segment(1) == 'nm' ? 'nm/' : '') . 'login');
            }
        }

        return $next($request);
    }
}

当我构建一个 7.x 应用程序时,它会生成:

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

我不确定如何继续。如果用户未在 Laravel 7 中进行身份验证,正确的重定向方法是什么?

【问题讨论】:

  • 你展示的方法是一种方法……你不确定呢?
  • 好吧,我不确定原始代码是否仍然可以在 Laravel 7 中工作。我已经走得更远了,并且已经能够测试它并且它可以工作,所以毕竟没有问题。感谢您的回复。

标签: laravel authentication redirect middleware


【解决方案1】:

旧代码在 Laravel 7 中仍然有效。

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 2021-01-03
    • 2023-03-16
    • 2014-08-11
    • 2013-06-14
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多