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

标签 authentication laravel-5 laravel-5.4 middleware laravel-middleware

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

我只能在构造函数内使用一个中间件,并且 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());
      }
 }

最佳答案

你可以在 Controller 中尝试这样

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

关于authentication - laravel 中只有一个函数的两个中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52113388/

相关文章:

php - Laravel 5.4 - 如何设置 PDO 获取模式?

php - 在login.php中输入成功的用户名和密码之前不断出现错误消息

security - 使用 AspNetUserTokens 表在 ASP.NET Core Web Api 中存储刷新 token

php - 获取我的 laravel 网站的所有链接以获取站点地图

laravel-5 - 在没有命令行监听器的情况下在 Laravel 5 中运行排队作业?

php - 如何跟踪 laravel 5 中的每个用户数据库交互?

php - Laravel:将数组从 Controller 传递给 mail::to

php - 重新加载后页面变成空白

c# - 使用c#生成不记名 token

php - Laravel 5 PDOException 找不到驱动程序