SomethingElse
SomethingElse

Reputation: 327

Bind middleware to controller in laravel 5

I want to use middleware in my controller , but I don't know route beforehand, as I take slug from my DB.

Is it possible in laravel 5?

Thank you in advance

Upvotes: 1

Views: 3745

Answers (1)

zeratulmdq
zeratulmdq

Reputation: 1494

Inside your controller's constructor, do the following:

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']]);
}

You can read about that here

Upvotes: 5

Related Questions