沃梦达 / 编程问答 / php问题 / 正文

如何在 Laravel 5.2 中使用多重身份验证

How to use multi Auth in laravel 5.2(如何在 Laravel 5.2 中使用多重身份验证)

本文介绍了如何在 Laravel 5.2 中使用多重身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何在 laravel 5.2 中使用多重身份验证!
我想使用它但我不知道如何使用?
有没有人有设置多重身份验证的教程或项目?

Does anyone know how to use multi authenticate in laravel 5.2 !
I want to use It but I don't know how ?
does anyone has a tutorial or project setting up multi authentication?

推荐答案

你需要两个表 usersadmins运行以下命令以创建内置身份验证

You need two tables users and admins Run command following command to create built in auth

php artisan make:auth

两个模型用户(已经存在)和管理员

Two models Users(Already exist) and Admin

<?php

namespace App;

use IlluminateFoundationAuthUser as Authenticatable;

class Admin extends Authenticatable
{
	
}

现在打开 config/auth.php 并进行以下更改

Now open config/auth.php and make the following changes

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppUser::class,
        ],

         'admins' => [
             'driver' => 'eloquent',
             'model' => AppAdmin::class,
         ],
    ],

'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
		'admins' => [
            'provider' => 'admins',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

创建一个新的中间件 RedirectIfNotAdmin

<?php

namespace AppHttpMiddleware;

use Closure;
use IlluminateSupportFacadesAuth;

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('/admin/login');
		}
	
		return $next($request);
	}
}

Kernel.php 中的变化

Changes in Kernel.php

protected $middleware = [
        IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
		IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
	];


protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            //IlluminateSessionMiddlewareStartSession::class,
            //IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
        ],
		
        'api' => [
            'throttle:60,1',
        ],
    ];


protected $routeMiddleware = [
        'auth' => AppHttpMiddlewareAuthenticate::class,
        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
		'admin' => AppHttpMiddlewareRedirectIfNotAdmin::class,
    ];

创建一个新文件夹Http/Controller/Adminauth并从Http/Controller/Auth文件夹中复制文件

Create a new folder Http/Controller/Adminauth and copy the files from Http/Controller/Auth folder

打开文件Http/Controller/Adminauth/AuthController.php,做如下修改

Open the file Http/Controller/Adminauth/AuthController.php and make the following changes

<?php

namespace AppHttpControllersAdminauth;

use AppAdmin;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
use Auth;

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = '/admin';
	protected $guard = 'admin';
	
	public function showLoginForm()
	{
		if (Auth::guard('admin')->check())
		{
			return redirect('/admin');
		}
		
		return view('admin.auth.login');
	}
	
	public function showRegistrationForm()
	{
		return view('admin.auth.register');
	}
	
	public function resetPassword()
	{
		return view('admin.auth.passwords.email');
	}
	
	public function logout(){
		Auth::guard('admin')->logout();
		return redirect('/admin/login');
	}
}

新建文件夹Http/Controller/admin,复制Http/Controller/文件夹下的Controller.php文件

Create new folder Http/Controller/admin, copy Controller.php file in the folder from Http/Controller/

新建文件Http/Controller/admin/employee.php

create new file Http/Controller/admin/employee.php

<?php

namespace AppHttpControllersadmin;


use IlluminateHttpRequest;
use IlluminateRoutingController;

use Auth;
use AppAdmin;

class Employee extends Controller
{
	public function __construct(){
        $this->middleware('admin');
   }
	
	public function index(){
		return view('admin.home');
    }
}

移动到资源/视图创建新文件夹资源/视图/管理复制

move to resources/views create new folder resources/views/admin copy

resources/views/auth、resources/views/layouts &资源/视图/home.blade.php

并发布到 resources/views/admin 并打开 admin 文件夹中的每个文件并在每个路径前添加 admin,现在路径应该看起来像

and post into resources/views/admin and open the each file in admin folder and add admin before each path, Now the path should look like

@extends('admin.layouts.app')

你的 Http/routes.php 看起来像

and your Http/routes.php look like

<?php
Route::get('/', function () {
    return view('welcome');
});

Route::get('/admin/login','AdminauthAuthController@showLoginForm');
Route::post('/admin/login','AdminauthAuthController@login');
Route::get('/admin/password/reset','AdminauthPasswordController@resetPassword');

Route::group(['middleware' => ['admin']], function () {
    //Login Routes...
    Route::get('/admin/logout','AdminauthAuthController@logout');
	
    // Registration Routes...
    Route::get('admin/register', 'AdminauthAuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminauthAuthController@register');

    Route::get('/admin', 'AdminEmployee@index');
});



Route::group(['middleware' => 'web'], function () {
    Route::auth();
	Route::get('/home', 'HomeController@index');
	
	
});

那就是在浏览器中打开您的网站并检查和管理员 yoursiteurl/admin

Thats it open your site in browser and check and for admin yoursiteurl/admin

享受....

这篇关于如何在 Laravel 5.2 中使用多重身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Laravel 5.2 中使用多重身份验证

基础教程推荐