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

问题描述 投票:2回答:1

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

我只能在构造函数中使用一个中间件,如果其他条件也不起作用

我只想要一个功能工作或根据中间件使用。我有两个单独的表进行身份验证

    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());
      }
 }
authentication laravel-5 laravel-5.4 middleware laravel-middleware
1个回答
2
投票

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

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',
    ],
];

更多:https://laravel.com/docs/master/middleware

© www.soinside.com 2019 - 2024. All rights reserved.