Authentication laravel中只有一个函数的两个中间件

Authentication laravel中只有一个函数的两个中间件,authentication,laravel-5,laravel-5.4,middleware,laravel-middleware,Authentication,Laravel 5,Laravel 5.4,Middleware,Laravel Middleware,我一次只能使用一个中间件..在构造函数内..我想在构造函数内使用if-else条件或两个中间件 我只能在构造函数中使用一个中间件&否则条件也不起作用 根据中间件,我只需要一个函数工作或使用。我有两个用于身份验证的seprate表 Following middleware pointing to diffrent table $this->middleware('auth:admin') - admins $this->middleware('

我一次只能使用一个中间件..在构造函数内..我想在构造函数内使用if-else条件或两个中间件

我只能在构造函数中使用一个中间件&否则条件也不起作用

根据中间件,我只需要一个函数工作或使用。我有两个用于身份验证的seprate表

    Following middleware pointing to diffrent table

       $this->middleware('auth:admin') - admins 
        $this->middleware('auth')- user
下面是一个例子

否则

class HRJob extends Controller
   {   
       public function __construct()
       {
          if(Auth::guard('admin'))
         {
          $this->middleware('auth:admin');
         }
       else
       {
       $this->middleware('auth');
        }
    }
      public function userdetails()
      {
     dd(Auth::user());
      }
    }
两个中间件

class HRJob extends Controller
 {  
   public function __construct()
       {
     $this->middleware('auth:admin');
     $this->middleware('auth');
     }

  public function userdetails()
     {
     dd(Auth::user());
      }
 }

您可以在控制器中这样尝试

class UserController extends Controller
{
    /**
     * Instantiate a new UserController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');

        $this->middleware('log', ['only' => [
            'fooAction',
            'barAction',
        ]]);

        $this->middleware('subscribed', ['except' => [
            'fooAction',
            'barAction',
        ]]);
    }
}
还可以使用中间件组

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
        'auth:api',
    ],
];
更多: