laravel5.2实现区分前后台用户登录的方法
程序员文章站
2024-03-06 14:33:08
1.前台登录
直接使用laravel自带的auth
php artisan make:auth
然后可以查看路由文件:
route::group...
1.前台登录
直接使用laravel自带的auth
php artisan make:auth
然后可以查看路由文件:
route::group(['middleware' => 'web'], function () { route::auth(); route::get('/home', 'homecontroller@index'); });
执行php artisan migrate
会发现生成了两张表。
2.后台登录
编辑配置文件
config\auth.php
添加guards中的admin和providers中的admins
<?php 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' => app\user::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => app\admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ], ];
3.创建数据模型
执行php artisan make:model admin --migration
然后修改database\migrations中的数据表结构,复制user表的就可以
执行php artisan migrate 会发现生成了admin表
4.定义后台路由
这里我直接定义了一个路由组
route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { route::get('login', 'admin\authcontroller@getlogin'); route::post('login', 'admin\authcontroller@postlogin'); route::get('register', 'admin\authcontroller@getregister'); route::post('register', 'admin\authcontroller@postregister'); route::get('logout', 'admin\authcontroller@logout'); route::get('/', 'admin\admincontroller@index'); });
5.创建控制器
执行
php artisan make:controller admin/authcontroller php artisan make:controller admin/admincontroller
authcontroller.php可参照auth中的authcontroller.php
<?php namespace app\http\controllers\admin; use app\admin; use validator; use app\http\controllers\controller; use illuminate\foundation\auth\throttleslogins; use illuminate\foundation\auth\authenticatesandregistersusers; class authcontroller extends controller { use authenticatesandregistersusers, throttleslogins; protected $redirectto = '/admin'; protected $guard = 'admin'; protected $loginview = 'admin.login'; protected $registerview = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } }
admincontroller.php
<?php namespace app\http\controllers\admin; use illuminate\http\request; use app\http\requests; use app\http\controllers\controller; use auth; class admincontroller extends controller { public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = auth::guard('admin')->user(); return view('admin.home'); } }
6.创建视图
这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。
修改login和register中表单的action,添加上admin前缀
修改layouts中的app.blade.php
<ul class="nav navbar-nav navbar-right"> <!-- authentication links --> @if (auth::guest('admin')) <li><a href="{{ url('/login') }}">login</a></li> <li><a href="{{ url('/register') }}">register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>logout</a></li> </ul> </li> @endif </ul>
现在试一下前后台分别登录吧!
7.可能遇到的页面跳转问题
如果你遇到了这种情况可以尝试修改一下middleware\authenticate.php
return redirect()->guest($guard.'/login');
以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助