我要创建管理员登录系统和用户登录系统。两者都有不同的表。对于用户身份验证,我使用普通身份验证,对于管理员身份验证,我使用中间件。我的代码在下面,请告诉我这段代码是什么错误
// RedirectIfNotAdmin
public function handle($request, Closure $next, $guard = 'admin')
{
if (!Auth::guard($guard)->check()) {
return redirect('/admin');
}
return $next($request);
}
路线
Route::group(['middleware' => ['web']], function () {
Route::get('admin/login', array('as' => 'login.index', 'uses' => 'Admin\LoginController@showLoginForm'));
Route::post('admin/login', array('as' => 'login.entry', 'uses' => 'Admin\LoginController@login'));
Route::get('/admin/logout','Admin\LoginController@logout');
Route::get('admin/register', 'Admin\RegisterController@showRegistrationForm');
Route::post('admin/register', 'Admin\RegisterController@register');
Route::get('/admin', 'AdminController@index');
});
内核
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
管理控制器
class AdminController extends Controller{
public function __construct(){
$this->middleware('admin');
}
public function index(){
return view('home');
}}
登录控制器
protected $redirectTo = '/admin';
public $guard = 'admin';
public function showLoginForm()
{
if (view()->exists('admin.auth.authenticate')) {
return view('admin.auth.authenticate');
}
return view('admin.auth.login');
}
public function showRegistrationForm()
{
return view('admin.auth.register');
}