我想验证用户和管理员的形式user
表,并admin
分别表.我正在使用User
开箱即用的laravel提供的模型,并为Admin.
我添加了一个保护密钥和提供者密钥创建了相同的模型auth.php.
逆天
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
供应商
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
路线
Route::group(['middleware' => ['web']], function () {
// Login Routes.
Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
Route::post('/admin/login','AdminAuth\AuthController@login');
Route::get('/admin/logout','AdminAuth\AuthController@logout');
// Registration Routes.
Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuth\AuthController@register');
Route::get('/admin', 'AdminController@index');
});
我创建了一个名为AuthAdmin
Laravel的默认目录AuthController.php
和PasswordController.php
文件的目录.(名称空间相应修改)
首先,在Laravel的文档中提到如何指定自定义防护,同时进行身份验证,这是不起作用的.
Laravel的文档中提到的另一种方法是使用不起作用的后卫.
如果有人可以解决问题并在我错了的情况下纠正我,那将是有益的.
经过大量的挖掘和大量的问答后,我终于设法使用两张桌子来完成Laravel 5.2 Multi Auth,所以我正在编写自己的问题答案.
如何在Larvel 5.2中实现Multi Auth
正如刚才提到的.两张桌子admin
和users
Laravel 5.2有一个新artisan
命令.
php artisan make:auth
它会产生基本的登录/注册route
,view
并controller
为user
表.
为简单起见,将admin
表格作为users
表格.
Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(注意:我刚从app/Http/Controllers/Auth/AuthController
这里复制了这些文件)
config/auth.php
//Authenticating guards 'guards' => [ 'user' =>[ 'driver' => 'session', 'provider' => 'user', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admin', ], ], //User Providers 'providers' => [ 'user' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admin' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ] ], //Resetting Password 'passwords' => [ 'clients' => [ 'provider' => 'client', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], 'admins' => [ 'provider' => 'admin', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],
route.php
Route::group(['middleware' => ['web']], function () { //Login Routes... Route::get('/admin/login','AdminAuth\AuthController@showLoginForm'); Route::post('/admin/login','AdminAuth\AuthController@login'); Route::get('/admin/logout','AdminAuth\AuthController@logout'); // Registration Routes... Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm'); Route::post('admin/register', 'AdminAuth\AuthController@register'); Route::get('/admin', 'AdminController@index'); });
AdminAuth/AuthController.php
添加两个方法并指定$redirectTo
和$guard
protected $redirectTo = '/admin'; protected $guard = 'admin'; public function showLoginForm() { if (view()->exists('auth.authenticate')) { return view('auth.authenticate'); } return view('admin.auth.login'); } public function showRegistrationForm() { return view('admin.auth.register'); }
它将帮助您为管理员打开另一个登录表单
为...创建中间件 admin
class RedirectIfNotAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = 'admin') { if (!Auth::guard($guard)->check()) { return redirect('/'); } return $next($request); }
}
注册中间件 kernel.php
protected $routeMiddleware = [ 'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class, ];
在AdminController
例如中使用此中间件
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; class AdminController extends Controller { public function __construct(){ $this->middleware('admin'); } public function index(){ return view('admin.dashboard'); } }
这一切都需要使它工作,并获得经过身份验证的管理员使用的json
Auth::guard('admin')->user()
编辑 - 1
我们可以直接使用经过身份验证的用户,
Auth::user()
但如果您有两个身份验证表,则必须使用
Auth::guard('guard_name')->user()
注销
Auth::guard('guard_name')->user()->logout()
用于经过身份验证的用户json
Auth::guard('guard_name')->user()
现在您可以下载Laravel 5.2 Multiauth实施项目http://imrealashu.in/code/laravel/multi-auth-with-laravel-5-2-2/