Laravel 5 - Defining middleware for multiple routes in controller file

Talky picture Talky · Feb 21, 2015 · Viewed 7k times · Source

Stackers! I'm currently learning laravel5 and I love it, but I'm struggling with one thing. Since Laravel 5 we have Middleware which we can use in controller's construct function, like this:

Controller file:

public function __construct()
{
    $this->middleware('admin', ['only' => 'create']);
}

Now what I want is to define HERE^ (not in routes file) middleware to be used in multiple views, like 'create', 'edit' and 'show'. defining

public function __construct()
{
            $this->middleware('admin', ['only' => 'create|edit|show']);
}

Unfortunately does not work. I'd rather not use routes. Any ideas, dear friends?

Answer

lukasgeiter picture lukasgeiter · Feb 21, 2015

Simply pass an array instead of a string with | delimiter:

public function __construct()
{
    $this->middleware('admin', ['only' => ['create', 'edit', 'show']]);
}