Talky
Talky

Reputation: 173

Laravel 5 - Defining middleware for multiple routes in controller file

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?

Upvotes: 17

Views: 7319

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153150

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

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

Upvotes: 27

Related Questions