处理程序类错误 - Laravel

时间:2017-09-21 09:26:08

标签: php laravel laravel-5 laravel-5.5

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

错误:

  

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

这是select t1.CalendarDate, u.[User], case when t2.[User] is null then 'Absent' else 'Present' end [Status], CAST(t2.wdt as date) WDT, CAST(t2.wdt as time) [Time] from Table1 t1 cross join (select distinct [User] from Table2) u left join Table2 t2 on t1.CalendarDate = CAST(t2.wdt as date) and u.[User] = t2.[User] CalendarDate User Status WDT Time 2017-09-22 User1 Present 2017-09-22 11:30:05.4800000 2017-09-23 User1 Present 2017-09-23 11:30:05.4800000 2017-09-24 User1 Absent NULL NULL 2017-09-25 User1 Absent NULL NULL 中我未经身份验证的功能:

app/Exceptions/Handler

请帮我解决此问题。

3 个答案:

答案 0 :(得分:19)

您忘记在文件顶部添加findNonZero()

答案 1 :(得分:2)

我正在使用Laravel 7.X
而且我更喜欢在身份验证中间件中进行此操作
我像波纹管一样做到了,它对我来说很好用。

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');
        }

    }

}

答案 2 :(得分:0)

感谢您最近的回答 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.* 及更高版本中弃用