一聚教程网:一个值得你收藏的教程网站

热门教程

laravel5.2实现区分前后台用户登录的方法

时间:2022-06-24 15:07:35 编辑:袖梨 来源:一聚教程网

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

Route::group(['middleware'=>'web'],function() {

 Route::auth();

 Route::get('/home','HomeController@index');

});

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

configauth.php

添加guards中的admin和providers中的admins

return[

 'defaults'=> [

  'guard'=>'web',

  'passwords'=>'users',

 ],

 'guards'=> [

  'web'=> [

   'driver'=>'session',

   'provider'=>'users',

  ],

  'admin'=> [

   'driver'=>'session',

   'provider'=>'admins',

  ],

  'api'=> [

   'driver'=>'token',

   'provider'=>'users',

  ],

 ],

 'providers'=> [

  'users'=> [

   'driver'=>'eloquent',

   'model'=> AppUser::class,

  ],

  'admins'=> [

   'driver'=>'eloquent',

   'model'=> AppAdmin::class,

  ],

 ],

 'passwords'=> [

  'users'=> [

   'provider'=>'users',

   'email'=>'auth.emails.password',

   'table'=>'password_resets',

   'expire'=> 60,

  ],

 ],

];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改databasemigrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix'=>'admin','middleware'=>'admin'],function() {

 Route::get('login','AdminAuthController@getLogin');

 Route::post('login','AdminAuthController@postLogin');

 Route::get('register','AdminAuthController@getRegister');

 Route::post('register','AdminAuthController@postRegister');

 Route::get('logout','AdminAuthController@logout');

 Route::get('/','AdminAdminController@index');

});

5.创建控制器

执行

php artisan make:controller Admin/AuthController

php artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

namespaceAppHttpControllersAdmin;

useAppAdmin;

useValidator;

useAppHttpControllersController;

useIlluminateFoundationAuthThrottlesLogins;

useIlluminateFoundationAuthAuthenticatesAndRegistersUsers;

classAuthControllerextendsController

{

 useAuthenticatesAndRegistersUsers, ThrottlesLogins;

 protected$redirectTo='/admin'

 protected$guard='admin'

 protected$loginView='admin.login'

 protected$registerView='admin.register'

 publicfunction__construct()

 {

  $this->middleware('guest:admin', ['except'=>'logout']);

 }

 protectedfunctionvalidator(array$data)

 {

  returnValidator::make($data, [

   'name'=>'required|max:255',

   'email'=>'required|email|max:255|unique:admins',

   'password'=>'required|confirmed|min:6',

  ]);

 }

 protectedfunctioncreate(array$data)

 {

  returnAdmin::create([

   'name'=>$data['name'],

   'email'=>$data['email'],

   'password'=> bcrypt($data['password']),

  ]);

 }

}

AdminController.php

namespaceAppHttpControllersAdmin;

useIlluminateHttpRequest;

useAppHttpRequests;

useAppHttpControllersController;

useAuth;

classAdminControllerextendsController

{

 publicfunction__construct()

 {

  $this->middleware('auth:admin');

 }

 publicfunctionindex()

 {

  $admin= Auth::guard('admin')->user();

  returnview('admin.home');

 }

}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。 

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

     

     @if(Auth::guest('admin'))

      

      

     @else

      

       

        {{ Auth::guard('admin')->user()->name }}

       

       

        

       

      

     @endif

    

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下MiddlewareAuthenticate.php

returnredirect()->guest($guard.'/login');

热门栏目