在Laravel 5中将中间件绑定到控制器

Som*_*lse 1 laravel laravel-5

我想在控制器中使用中间件,但是我事先不知道路由,因为我从数据库中获取了数据。

laravel 5中有可能吗?

先感谢您

小智 5

在控制器的构造函数中,执行以下操作:

public function __construct()
{
    //This will apply to every method inside the controller
    $this->middleware('auth');

    //This will apply only to the methods listed inside the array
    $this->middleware('log', ['only' => ['fooAction', 'barAction']]);

    //This will apply only to the methods except the ones listed inside the array
    $this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读