当前位置:  开发笔记 > 编程语言 > 正文

任何人都可以通过示例解释Laravel 5.2 Multi Auth

如何解决《任何人都可以通过示例解释Laravel5.2MultiAuth》经验,为你挑选了1个好方法。

我想验证用户管理员的形式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');
});

我创建了一个名为AuthAdminLaravel的默认目录AuthController.phpPasswordController.php文件的目录.(名称空间相应修改)

首先,在Laravel的文档中提到如何指定自定义防护,同时进行身份验证,这是不起作用的.
在此输入图像描述

Laravel的文档中提到的另一种方法是使用不起作用的后卫.

在此输入图像描述

如果有人可以解决问题并在我错了的情况下纠正我,那将是有益的.



1> imrealashu..:

经过大量的挖掘和大量的问答后,我终于设法使用两张桌子来完成Laravel 5.2 Multi Auth,所以我正在编写自己的问题答案.

如何在Larvel 5.2中实现Multi Auth

正如刚才提到的.两张桌子adminusers

Laravel 5.2有一个新artisan命令.

php artisan make:auth

它会产生基本的登录/注册route,viewcontrolleruser表.

为简单起见,将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()  

编辑2

现在您可以下载Laravel 5.2 Multiauth实施项目http://imrealashu.in/code/laravel/multi-auth-with-laravel-5-2-2/

推荐阅读
牛尾巴2010
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有