最近群里很多人文檔,laravel如何做會(huì)員和管理兩個(gè)身份登錄,今天把教程分享一下
自定義用戶(hù)表登錄
認(rèn)證是由 guards 和 providers 兩部分構(gòu)成的, defaults 配置是默認(rèn)選擇一個(gè) guard 認(rèn)證驅(qū)動(dòng),所以我們?cè)谶@兩個(gè)配置項(xiàng)中分別添加一個(gè) admin 和 admins 選項(xiàng)。
?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,
],
],
標(biāo)紅的是我們后添加的后臺(tái)管理員登錄身份
創(chuàng)建后臺(tái)用戶(hù)表和model
php artisan make:model Admin
php artisan make:migration creaet_admins_table
在數(shù)據(jù)庫(kù)遷移文件 _create_admins_table , 我們可以復(fù)制 users 遷移文件里的字段
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
執(zhí)行php artisan migrate
生成臨時(shí)數(shù)據(jù)
在 database/factories/ModelFactory.php, 添加如下數(shù)據(jù):
$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),
];
});
打開(kāi)命令行輸入:
php artisan tinker
use App;
factory(App\Admin::class,5)->create()
//生成5條測(cè)試數(shù)據(jù),你要幾條就輸入多少(Class,num)
更改 Admin 模型類(lèi)
?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',
];
}
創(chuàng)建控制器
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');
}
/**
* 重寫(xiě)登錄視圖頁(yè)面
*/
public function showLogin()
{
return view('admin.login.index');
}
/**
* 自定義認(rèn)證驅(qū)動(dòng)
* @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()) {
// 根據(jù)不同 guard 跳轉(zhuǎn)到不同的頁(yè)面
$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('用戶(hù)名:'.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 中注冊(cè):
protected $routeMiddleware = [
··· ···
'auth.admin' => \App\Http\Middleware\AdminAuthMiddleware::class,
];
注冊(cè)路由
編輯 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');
});
視圖文件創(chuàng)建和修改
復(fù)制 resources\views\auth\login.blade.php,到 resources\views\admin\login\index.blade.php,修改表單提交地址
{{ url('/login') }} 改成 {{ route('admin.login') }}
訪問(wèn) 你的站點(diǎn)/admin/login
以上這篇Laravel5.5 實(shí)現(xiàn)后臺(tái)管理登錄的方法(自定義用戶(hù)表登錄)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 解決laravel5中auth用戶(hù)登錄其他頁(yè)面獲取不到登錄信息的問(wèn)題
- laravel實(shí)現(xiàn)Auth認(rèn)證,登錄、注冊(cè)后的頁(yè)面回跳方法
- Laravel 自帶的Auth驗(yàn)證登錄方法
- Laravel搭建后臺(tái)登錄系統(tǒng)步驟詳解
- Laravel實(shí)現(xiàn)用戶(hù)注冊(cè)和登錄
- laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)