编程学习网 > PHP技术 > laravel > Laravel5.5 实现后台管理登录的方法
2020
02-08

Laravel5.5 实现后台管理登录的方法

最近群里很多人文档,laravel如何做会员和管理两个身份登录,今天把教程分享一下
自定义用户表登录
认证是由 guards 和 providers 两部分构成的, defaults 配置是默认选择一个 guard 认证驱动,所以我们在这两个配置项中分别添加一个 admin 和 admins 选项。
<?php 
 'guards' => [ 
  'web' => [ 
   'driver' => 'session', 
   'provider' => 'users', 
  ], 
  'admin' => [ 
   'driver' => 'session', 
   'provider' => 'admins', 
  ], 
 'providers' => [ 
  'users' => [ 
   'driver' => 'eloquent', 
   'model' => App\User::class, 
  ], 
  'admins' => [ 
   'driver' => 'eloquent', 
   'model' => App\Models\Admin::class, 
  ], 
 ],

标红的是我们后添加的后台管理员登录身份
创建后台用户表和model

php artisan make:model Admin 
php artisan make:migration creaet_admins_table

在数据库迁移文件 _create_admins_table , 我们可以复制 users 迁移文件里的字段
Schema::create('admins', function (Blueprint $table) { 
   $table->increments('id'); 
   $table->string('name'); 
   $table->string('email'); 
   $table->string('password'); 
   $table->rememberToken(); 
   $table->timestamps(); 
  });

执行php artisan migrate
生成临时数据
在 database/factories/ModelFactory.php, 添加如下数据:
$factory->define(App\Admin::class, function (Faker\Generator $faker) { 
 static $password; 
  
 return [ 
  'name' => $faker->name, 
  'password' => $password ?: $password = bcrypt('123456'), 
  'email' => $faker->email, 
  'remember_token' => str_random(10), 
 ]; 
});

打开命令行输入:

php artisan tinker 
 use App; 
 factory(App\Admin::class,5)->create() 
 //生成5条测试数据,你要几条就输入多少(Class,num)

更改 Admin 模型类

<?php 
  
namespace App; 
  
use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 
  
class Admin extends Authenticatable 
{ 
 use Notifiable; 
  
 /** 
  * The attributes that are mass assignable. 
  * 
  * @var array 
  */
 protected $fillable = [ 
  'name', 'email', 'password', 
 ]; 
  
 /** 
  * The attributes that should be hidden for arrays. 
  * 
  * @var array 
  */
 protected $hidden = [ 
  'password', 'remember_token', 
 ]; 
} 

创建控制器

php artisan make:controller Admin/LoginController 
php artisan make:controller Admin/AdminController

编辑 Admin/LoginController.php:

<?php 
  
namespace App\Http\Controllers\Admin; 
  
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 
  
class LoginController extends Controller 
{ 
 use AuthenticatesUsers; 
  
 /** 
  * Where to redirect users after login / registration. 
  * 
  * @var string 
  */
 protected $redirectTo = '/admin/index'; 
 protected $username; 
  
 /** 
  * Create a new controller instance. 
  * 
  * @return void 
  */
 public function __construct() 
 { 
  $this->middleware('guest:admin', ['except' => 'logout']); 
  $this->username = config('admin.global.username'); 
 } 
  
 /** 
  * 重写登录视图页面 
  */
 public function showLogin() 
 { 
  return view('admin.login.index'); 
 } 
  
 /** 
  * 自定义认证驱动 
  * @return mixed 
  */
 protected function guard() 
 { 
  return auth()->guard('admin'); 
 } 
  
  
} 

修改 app\Http\Middleware\RedirectIfAuthenticated.php:

public function handle($request, Closure $next, $guard = null) 
 { 
  if (Auth::guard($guard)->check()) { 
   // 根据不同 guard 跳转到不同的页面 
   $url = $guard ? 'admin/dash':'/home'; 
   return redirect($url); 
  } 
  
  return $next($request); 
 } 

编辑 Admin\AdminController.php:

<?php 
  
namespace App\Http\Controllers\Admin; 
  
use App\Http\Controllers\Controller; 
  
class AdminController extends Controller 
{ 
 /** 
  * Create a new controller instance. 
  * 
  * @return void 
  */
 public function __construct() 
 { 
  $this->middleware('auth.admin:admin'); 
 } 
 // 
 public function index() 
 { 
  dd('用户名:'.auth('admin')->user()->name); 
 } 

编辑 app\Http\Middleware\AdminAuthMiddleware.php

public function handle($request, Closure $next, $guard = null) 
 { 
  if (Auth::guard($guard)->guest()) { 
   if ($request->ajax() || $request->wantsJson()) { 
    return response('Unauthorized.', 401); 
   } else { 
    return redirect()->guest('admin/login'); 
   } 
  } 
  return $next($request); 
 } 

在 app\Http\Kernel.php 中注册:

protected $routeMiddleware = [ 
  ••• ••• 
  'auth.admin' => \App\Http\Middleware\AdminAuthMiddleware::class, 
 ]; 

注册路由
编辑 routes/web.php :

Route::group(['prefix' => 'admin','namespace' => 'Admin'],function ($router) 
{ 
 $router->get('login', 'LoginController@showLogin')->name('admin.login'); 
 $router->post('login', 'LoginController@login'); 
 $router->post('logout', 'LoginController@logout'); 
  
 $router->get('index', 'AdminController@index'); 
});

视图文件创建和修改
复制 resources\views\auth\login.blade.php,到 resources\views\admin\login\index.blade.php,修改表单提交地址

{{ url('/login') }} 改成 {{ route('admin.login') }}
访问 你的站点/admin/login
以上这篇Laravel5.5 实现后台管理登录的方法(自定义用户表登录)就是小编分享给大家的全部内容了

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取