概述
最近群里很多人文档,laravel如何做会员和管理两个身份登录,今天把教程分享一下
自定义用户表登录
认证是由 guards 和 providers 两部分构成的, defaults 配置是默认选择一个 guard 认证驱动,所以我们在这两个配置项中分别添加一个 admin 和 admins 选项。
<?php 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => AppUser::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => AppModelsAdmin::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(AppAdmin::class, function (FakerGenerator $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(AppAdmin::class,5)->create() //生成5条测试数据,你要几条就输入多少(Class,num)
更改 Admin 模型类
<?php namespace App; use IlluminateNotificationsNotifiable; use IlluminateFoundationAuthUser 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 AppHttpControllersAdmin; use IlluminateHttpRequest; use AppHttpControllersController; use IlluminateFoundationAuthThrottlesLogins; use IlluminateFoundationAuthAuthenticatesUsers; 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'); } }
修改 appHttpMiddlewareRedirectIfAuthenticated.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); }
编辑 AdminAdminController.php:
<?php namespace AppHttpControllersAdmin; use AppHttpControllersController; 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); }
编辑 appHttpMiddlewareAdminAuthMiddleware.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); }
在 appHttpKernel.php 中注册:
protected $routeMiddleware = [ ··· ··· 'auth.admin' => AppHttpMiddlewareAdminAuthMiddleware::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'); });
视图文件创建和修改
复制 resourcesviewsauthlogin.blade.php,到 resourcesviewsadminloginindex.blade.php,修改表单提交地址
{{ url('/login') }} 改成 {{ route('admin.login') }}
访问 你的站点/admin/login
以上这篇Laravel5.5 实现后台管理登录的方法(自定义用户表登录)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持靠谱客。
最后
以上就是愤怒巨人为你收集整理的Laravel5.5 实现后台管理登录的方法(自定义用户表登录)的全部内容,希望文章能够帮你解决Laravel5.5 实现后台管理登录的方法(自定义用户表登录)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复