as the title says I want to use a middleware inside a controller function. I have resource controllers, which their functions inside will have different access rights so I can't use a middleware in the web.php file, I have to use or apply it separately in each function to limit access, my googling hasn't been successful in getting a solution to that so far. Any help please and thanks in advance.
P.S. I believe no code is necessary here.
Middleware could also be applied to just one function, just add the method name in your controller constructor
public function __construct()
{
// Middleware only applied to these methods
$this->middleware('loggedIn', ['only' => [
'update' // Could add bunch of more methods too
]]);
}
Here's the documentation
Use the following code inside your controller constructor. The following code will use the auth middleware:
public function __construct() {
$this->middleware('auth');
}
Also you can simply add middleware at your routes. For example I need to add middleware for my method "registration_fee()" inside "RegisterController", so it will looks like this:
Route::get('/pay_register_fee', 'Auth\RegisterController@registration_fee')
->name('pay_register_fee')->middleware(['guest', Register::class, RegistrationFee::class]);
"RegistrationFee" is middleware that I want to add.
P.S. Not forget import class or write full path to middleware.