【发布时间】:2019-09-18 00:16:51
【问题描述】:
我在我的应用程序中创建了一个自定义守卫(管理员),按照本教程在线 (https://pusher.com/tutorials/multiple-authentication-guards-laravel) 一切看起来都很好。唯一的问题是当我尝试访问受管理员保护的视图时,而不是给我一个异常 NotAuthenticate,而是给我一个“InvalidArgumentException Route [login] 未定义。”。
我的仪表板控制器自定义守卫是:
public function __construct()
{
$this->middleware('auth:admin');
}
但我无法理解发生了什么奇怪的事情是,当我在 web.php 中添加“Auth::routes();”路由时,它工作正常,异常 NotAuthenticate 被触发。
我添加 Auth::routes() 时,我的自定义仅适用于预期是否有原因?
管理员登录控制器:
namespace App\Http\Controllers\Admin\Auth;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Validation\ValidationException;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/admin/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest:admin')->except('logout');
}
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
public function showLoginForm()
{
return view('admin.auth.login');
}
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return $this->loggedOut($request) ?: redirect('/admin');
}
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard('admin');
}
}
【问题讨论】:
-
我遇到了同样的问题。能否请您显示您的登录方法。 AdminLoginController 扩展了什么控制器?
-
当然@dexter,我刚刚从管理员更新并添加了登录控制器