任何人都可以通过示例解释 Laravel 5.2 多重身份验证 [英] Can Anyone Explain Laravel 5.2 Multi Auth with Example
问题描述
我正在尝试分别验证 users 和 admin 表单 user
表和 admin
表.我正在使用 laravel 开箱即用的 User
模型,并为 Admin 创建了相同的模型.
我在 auth 中添加了一个保护密钥和提供者密钥.
I am trying to authenticate users and admin form user
table and admin
table respectively. I am using the User
model as provided by laravel out of the box and created the same for Admin.
I have added a guard key and provider key into auth.php.
守卫
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
供应商
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => AppUser::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => AppAdmin::class,
]
],
路线
Route::group(['middleware' => ['web']], function () {
// Login Routes.
Route::get('/admin/login','AdminAuthAuthController@showLoginForm');
Route::post('/admin/login','AdminAuthAuthController@login');
Route::get('/admin/logout','AdminAuthAuthController@logout');
// Registration Routes.
Route::get('admin/register', 'AdminAuthAuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuthAuthController@register');
Route::get('/admin', 'AdminController@index');
});
我创建了一个名为 AuthAdmin
的目录,其中存在 Laravel 默认的 AuthController.php
和 PasswordController.php
文件.(命名空间相应修改)
I have created a directory called AuthAdmin
where Laravel's default AuthController.php
and PasswordController.php
files are present. (Namespace Modified accordingly)
首先,在 Laravel 的文档中提到如何在像这样进行身份验证时指定自定义守卫是行不通的.
First of all, in Laravel's docs mentioned that how to specify custom guard while authenticating like this which isn't working.
在 Laravel 的文档中提到了另一种使用守卫的方法,但它也不起作用.
There's another method mentioned in Laravel's docs to use a guard which is not working too.
如果有人能够解决问题并在我错了时纠正我,那将是有益的.
It would be beneficial if someone could resolve the issues and correct me if I am wrong.
推荐答案
经过大量挖掘和大量问题 &我终于设法使用两个表来处理 Laravel 5.2 Multi Auth,所以我正在写我自己的问题的答案.
After lots of digging and lots of questions & answers I have finally managed to work Laravel 5.2 Multi Auth with two table, So I'm writing Answer of my own Question.
如何在 Laravel 5.2 中实现多重身份验证
如上所述.两个表 admin
和 users
As Mentioned above.
Two table admin
and users
Laravel 5.2 有一个新的 artisan
命令.
Laravel 5.2 has a new artisan
command.
php artisan make:auth
它将为user
表生成基本的登录/注册route
、view
和controller
.
it will generate basic login/register route
, view
and controller
for user
table.
为简单起见,将 admin
表设为 users
表.
Make a admin
table as users
table for simplicity.
管理员控制器app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(注意:我只是从 app/Http/Controllers/Auth/AuthController
这里复制了这些文件)
Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthController
here)
config/auth.php
//Authenticating guards
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => AppUser::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => AppAdmin::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','AdminAuthAuthController@showLoginForm');
Route::post('/admin/login','AdminAuthAuthController@login');
Route::get('/admin/logout','AdminAuthAuthController@logout');
// Registration Routes...
Route::get('admin/register', 'AdminAuthAuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuthAuthController@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');
}
它将帮助您为管理员打开另一个登录表单
it will help you to open another login form for admin
为admin
class RedirectIfNotAdmin
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $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' => AppHttpMiddlewareRedirectIfNotAdmin::class,
];
在AdminController
中使用这个中间件例如,
use this middleware in AdminController
e.g.,
这篇关于任何人都可以通过示例解释 Laravel 5.2 多重身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!