Laravel开发:多用户登录验证(2)
上一篇讲了最基本的User验证,现在来讲一下Admin的验证。
先贴代码,
路由:routes/web.php加上以下代码,
//...
Route::get('admin/login', 'Admin\AuthController@showLoginForm');
Route::post('admin/login', 'Admin\AuthController@login');
Route::get('admin/register', 'Admin\AuthController@showRegistrationForm');
Route::post('admin/register', 'Admin\AuthController@register');
Route::post('admin/logout', 'Admin\AuthController@logout');
Route::get('admin', 'AdminController@index');
中间件:修改 config/auth.php 配置如下,
<?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,//根据需要设置命名空间名
//'model' => App\Models\Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
控制器:
app/Http/Controllers/Admin/AuthController.php
代替
app/Http/Controllers/Auth/RegisterController.php 与 app/Http/Controllers/Auth/LoginController.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\AuthenticatesUsers; use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
//use Illuminate\Foundation\Auth\RegistersUsers; class AuthController extends Controller
{
//use AuthenticatesUsers, ThrottlesLogins; //use RegistersUsers; use AuthenticatesUsers; protected $redirectTo = '/admin';
protected $guard = 'admin';
protected $loginView = 'admin.login';
protected $registerView = 'admin.register'; public function __construct()
{ $this->middleware('guest:admin', ['except' => 'logout']);
//$this->middleware('guest:admin')->except('logout');
//$this->middleware('auth');
} public function showRegistrationForm(){
return view('admin.register');
} public function register(Request $request){
$this->validator($request->all())->validate(); event(new Registered($user = $this->create($request->all()))); $this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
} protected function registered(Request $request, $user)
{
//
} protected function guard()
{
return Auth::guard('admin');
} public function showLoginForm()
{
return view('admin.login');
} public function login(Request $request){
$this->validateLogin($request); // If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request); return $this->sendLockoutResponse($request);
} if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
} // If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request);
} public function username()
{
return 'email';
} public function logout(Request $request)
{
$this->guard()->logout(); $request->session()->flush(); $request->session()->regenerate(); return redirect('/');
} protected function validateLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|string',
'password' => 'required|string',
]);
} protected function validator(array $data)
{ return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:admin',
'password' => 'required|confirmed|min:6',
]); } protected function create(array $data)
{
return Admin::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]); } }
app/Http/Controllers/Admin/AdminController.php
代替
app/Http/Controllers/Home/HomeController.php 所负责的登陆后跳转等业务逻辑
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth; class AdminController extends Controller
{ protected $redirectTo = '/admin'; protected $guard = 'admin'; public function __construct()
{
$this->middleware('auth:admin');
//$this->middleware('guest:admin', ['except' => 'logout']);
} public function index()
{
return view('admin.home');
//$admin = \Illuminate\Support\Facades\Auth::guard('admin')->user();
//return $admin->name;
} }
Table表: admin

CREATE TABLE `mgshop_admin` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Model:app/Admin.php
<?php namespace App; use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContracts; class Admin extends Model implements AuthenticatableContracts
{
use Authenticatable; protected $table = 'admin';//'admin';//设置表名
protected $primaryKey = 'id';//'AdminID';//设置主键
public $timestamps = false;
protected $fillable = ['name','email','password'];//开启白名单字段
}
app/Http/Middleware/RedirectIfAuthenticated.php(在构造函数中修改 guest 中间件,用来跳转不同路由:)
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
// 根据不同 guard 跳转到不同的页面
$url = $guard ? 'admin':'/home';
return redirect($url);
}
/* if (Auth::guard($guard)->check()) {
return redirect('/home');
} */ return $next($request);
}
域名+/login
域名+/admin/login
访问测试!
此链接有类似且详细的实现方式,可做参考。Laravel 5.3 多用户表登录实现:http://laravelacademy.org/post/5925.html
版权声明:本文为博主原创文章,未经博主允许不得转载。
Laravel开发:多用户登录验证(2)的更多相关文章
- Laravel开发:多用户登录验证(1)
之前实现了一次,后来代码忘记放哪了,所以有跳了一次坑. 先贴上Laravel自带的验证代码: 路由:routes/web.php // Authentication Routes... $this-& ...
- Android开发之登录验证
最近在做一个小项目,项目开发中需要实现一个登录验证功能,具体的要求就是,在Android端输入用户名和密码,在服务器端验证MySQL数据库中是否有此用户,实现之前当然首要的是,如何使Android端的 ...
- laravel后台账户登录验证(5.5.48版本)
首先我是菜鸟,对laravel框架也不是很熟悉,突然有一天心血来潮就想研究一下laravel的后台登录用户登录的流程, 虽然公司项目中有这样的一套流程,也看了好几遍,越看越简单,越看我就越会了,当自己 ...
- Axure实现多用户注册验证
*****多用户登录验证***** 一.(常规想法)方法:工作量较大,做起来繁琐 1.当用户名和密码相同时怎么区分两者,使用冒号和括号来区分: eg. (admin:123456)(123456:de ...
- Laravel登录验证碰到的坑 哈希验证匹配问题
用laravel 写登录验证 本来是用Crypt加密 添加用户到数据库的 后来验证密码 解密时一直报错 The payload is invaild 由于本人是laravel框架小白 自己思考许久未 ...
- 基于 Laravel 开发博客应用系列 —— 设置 Linux/Mac 本地开发环境
1.不同 Linux 发行版本的区别 不同的 Linux 发行版本之间有一些细微区别,尤其是包管理器:CentOS 和 Fedora 使用 yum 作为包管理器,而Ubuntu 使用 apt,在 O ...
- laravel 开发辅助工具
laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...
- 手摸手教你让Laravel开发Api更得心应手
https://www.guaosi.com/2019/02/26/laravel-api-initialization-preparation/ 1. 起因 随着前后端完全分离,PHP也基本告别了v ...
- 如何使用laravel搭建后台登录系统
今天想用laravel搭建一个后台系统,就需要最简单的那种,有用户登录系统,试用了下,觉得laravel的用户登录这块做的还真happy.当然,前提就是,你要的用户管理系统是最简单的那种,就是没有用户 ...
随机推荐
- sql cast函数
一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQLServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的数据,在 ...
- apache 配置防盗
防盗链目的:防止其他网站盗用自己的网站而增加额外的流量损失 SetEnvIfNoCase Referer "^http://.*\.yourdomin\.com" local_re ...
- Java高级特性—并发包
1). java并发包介绍 JDK5.0 以后的版本都引入了高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的, 主要包含原子量.并发集合.同步器.可 ...
- 初识Nginx及编译安装Nginx
初识Nginx及编译安装Nginx 环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 nginx-1.12.2 1.什么是Nginx? 如果你听说或使用过Apache软件 ...
- Solidworks如何在装配图中保存单独的一个零件
如下图所示,我想要保存装配体的一个单独的零部件 选中该零件后点击编辑零部件 然后点击顶部的文件-另存为,弹出"解决模糊情形"对话框,询问你要保存装配体还是零部件 点击确 ...
- 不是书评 :《我是一只IT小小鸟》
本文转自刘未鹏 博客,写的非常的好 就转回来了 设计你自己的进度条 进度条的设计是一个很多人都知道的故事:同样的耗时,如果不给任何进度提示,只是在完成之后才弹出一个完成消息,中间没有任何动态变化,那么 ...
- C2:抽象工厂 Abstract Factory
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 应用场景: 一系列相互依赖的对象有不同的具体实现.提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合 UM ...
- nginx静态文件缓存
open_file_cache max=65535 inactive=30s; open_file_cache 打开缓存的同时也指定了缓存最大数目,以及缓存的时间 open_file_cache_va ...
- 【转载】 json字符串与JSON对象
ajax中,我们自己拼接的是一个JSON对象,以为它是无数据类型的,所以JS根据其格式默认其实对象, 你要是往后台发,要先把它装换成JSON字符. 从ajax的服务器发过的,一定是字符串,你想要把它解 ...
- iconfont的简单使用
下载-阿里巴巴矢量图标 网站链接:http://www.iconfont.cn/ 首页如下: 首页-进入图标库--所有图标--搜索/点击你想要的图标--添加购物车 点击购物车(下载) 如下图: 点击下 ...