【问题标题】:Error in Handler Class - Laravel处理程序类中的错误 - Laravel
【发布时间】:2018-03-02 14:03:38
【问题描述】:

我正在使用 Laravel 5.5 并尝试为用户和管理员实现多重身份验证。当我尝试在浏览器中调用管理员登录表单时出现此错误。

错误:

App\Exceptions\Handler::unauthenticated($request, App\Exceptions\AuthenticationException $exception) 的声明应该与 Illuminate\Foundation\Exceptions\Handler::unauthenticated($request, Illuminate\Auth\AuthenticationException $exception 兼容)

这是我在app/Exceptions/Handler 中的未经身份验证的函数:

 protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
        $guard = array_get($exception->guards(), 0);
        switch ($guard) {
            case 'admin':
                $login = 'admin.login';
                break;
            default:
                $login = 'login';
                break;
        }
        return redirect()->guest(route($login));
    }

请帮我解决这个问题。

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.5


    【解决方案1】:

    感谢您最近的回答 Thanh Nguyen。我的自定义身份验证中间件适用于最新版本

     if (Arr::first($this->guards) === 'admin') {
           return route('admin.login');
      }
     return route('customer.login');
    

    之前在Handler.php中使用未经认证的函数来替换父函数。

     protected function unauthenticated($request, AuthenticationException $exception)
    {
        $guard = Arr::get($exception->guards(), 0);
        switch ($guard) {
          case 'respondent':
          $login = 'respondents.login';
          break;
          case 'admin':
          $login = 'admin.login';
          break;
          default:
          $login = 'admin.login';
          break;
        }
    
        return $request->expectsJson()
                    ? response()->json(['message' => $exception->getMessage()], 401)
                    : redirect()->guest(route($login));
    }
    

    两者都在工作,但在 array_get 上获取我们使用的保护的最新版本可能存在问题:

    $guard = array_get($exception->guards(), 0);
    

    对于遇到此问题的任何人,此方法已被 Laravel 7.* 及更高版本弃用

    【讨论】:

      【解决方案2】:

      我正在使用 Laravel 7.X
      我更喜欢在 Authenticate 中间件中这样做
      我像下面那样做了,它对我来说效果很好。

      namespace App\Http\Middleware;
      
      use Closure;
      use Illuminate\Auth\Middleware\Authenticate as Middleware;
      use Illuminate\Support\Arr;
      
      class Authenticate extends Middleware
      {
          protected $guards = [];
           /**
           * Handle an incoming request.
           *
           * @param  \Illuminate\Http\Request $request
           * @param  \Closure $next
           * @param  string[] ...$guards
           * @return mixed
           *
           * @throws \Illuminate\Auth\AuthenticationException
           */
          public function handle($request, Closure $next, ...$guards)
          {
              $this->guards = $guards;
      
              return parent::handle($request, $next, ...$guards);
          }
          /**
           * 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()) {
                  if (Arr::first($this->guards) === 'admin') {
                      return route('admin.login');
                  }
                  return route('trainee.login');
              }
      
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        您忘记在文件顶部添加use Illuminate\Auth\AuthenticationException

        【讨论】:

        • 嗨@Nerea,我知道它很老的答案,但我已经包含了这个但仍然给我这个错误
        猜你喜欢
        • 2015-03-29
        • 2016-08-18
        • 2015-01-05
        • 2018-12-13
        • 1970-01-01
        • 2018-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多