簡體   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