简体   繁体   English

如何在同一个控制器上使用laravel Multiple Auth Guard

[英]How to use laravel Multiple Auth Guard for same controller

laravel 5.2 laravel 5.2

I have and multiple auth Gard given below 我有以下多个auth Gard

Admin
Clients
Employee

i have 我有

    
ItemController
        ->index.blade.php
        ->create.blade.php
        ->edit.blade.php

ItemKitController
        ->index.blade.php
        ->create.blade.php
        ->edit.blade.php

I want to use Client and Employee Guard to access same Controller and view mention above. 我想使用Client和Employee Guard来访问相同的Controller并查看上面提到的内容。

is their any possible way. 是他们任何可能的方式。

maybe you want something like this 也许你想要这样的东西

public function __construct()
    {
        $this->middleware('auth:Admin,Clients,Employee');
    }

in your controller 在你的控制器中

You can use middleware like: 您可以使用以下中间件:

Route::group([ 'middleware' => ['Admin', 'Clients', 'Employee'] ], function(){
  Route::get('/Admin', 'AdminController@index');
  Route::get('/Clients', 'ClientsController@index');
  Route::get('/Employee', 'EmployeeController@index');

});

For instance, I have an admin middleware that checks if user id is 1 例如,我有一个管理中间件,用于检查用户ID是否为1

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use Log;

class AuthAdmin
{
    private $admins; // Admin ids

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $this->admins = config('custom.admins'); // get configs
        $user = Auth::user();

        if($user->id != 1)){
            // not admin, redirect home
            return redirect('/');
        }

      // is admin, let request continue
      return $next($request);
    }
}

Then you have to add it to Kernel.php "$routeMiddleware": 然后你必须将它添加到Kernel.php“$ routeMiddleware”:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

        // Custom Middleware
        // Auth Admin
        'auth_admin' => \App\Http\Middleware\AuthAdmin::class,
    ];

Then in my routes: 然后在我的路线:

Route::group([ 'middleware' => ['auth_admin'] ], function(){

    // nobody can come to these routes but admins
    Route::get('/admin/index', 'AdminController@index');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM