有人知道如何在laravel 5.8.15(或简单的5.8(中的 redirectTo()
方法中使用守卫?
我得到此错误
" app http middleware authenticate :: redirectto的声明($ request,$ guards(应与Illuminate airth airdware midderware authenticate authenticate euthenticate :: redirectto($ request(兼容
use IlluminateSupportFacadesRoute;
protected function redirectTo($request, $guards)
{
if (! $request->expectsJson()) {
if (array_first($this->guards) === 'admin') {
return route('admin.login');
}
return route('login');
}
}
//Updated code but still getting error
//Error:: Declaration of AppHttpMiddlewareAuthenticate::handle($request,
Closure $next, $guard = NULL) should be compatible with
IlluminateAuthMiddlewareAuthenticate::handle($request, Closure $next,
...$guards)
//Code
....
use Closure;
use IlluminateSupportFacadesAuth;
....
public function handle($request, Closure $next, $guard = null)
{
switch ($guard) {
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin.login');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/login');
}
break;
}
return $next($request);
}
这样检查,并重定向您的状况
public function handle($request, Closure $next)
{
switch ($this->getGuard()) {
case 'admin':
if (!Auth::guard($guard)->check()) {
return redirect()->route('admin_login');
}
break;
case 'vendor':
if (!Auth::guard($guard)->check()) {
return redirect()->route('vendor_login');
}
break;
case 'user':
if (Auth::guard($guard)->check()) {
return redirect()->route('user_login');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/login');
}
break;
}
return $next($request);
}
修改App/Exceptions/Handler.php
中的render($request, Exception $exception)
看起来像下面的一个:
/**
* Render an exception into an HTTP response.
*
* @param IlluminateHttpRequest $request
* @param Exception $exception
* @return IlluminateHttpResponse
*/
public function render($request, Exception $exception)
{
if(get_class($exception) != 'IlluminateAuthAuthenticationException'){
return parent::render($request, $exception);
}
$guard = Arr::get($exception->guards(),0);
switch ($guard){
case 'admin':
return redirect(route('admin.login'));
break;
default:
return redirect(route('login'));
break;
}
}