繁体   English   中英

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

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

laravel 5.2

我有以下多个auth Gard

Admin
Clients
Employee

我有

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

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

我想使用Client和Employee Guard来访问相同的Controller并查看上面提到的内容。

是他们任何可能的方式。

也许你想要这样的东西

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

在你的控制器中

您可以使用以下中间件:

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

});

例如,我有一个管理中间件,用于检查用户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);
    }
}

然后你必须将它添加到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,
    ];

然后在我的路线:

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