How is the best way to register some module middeware on the Laravel?
In your service provider you can call the middleware method on the Router class ($this->app['router'])
Some changes came into Laravel 5.4. For router middleware you need to use aliasMiddleware method on $this->app['router'] and you are ready to go.
So I cant load middleware in my service provider module?
The only solution is to mannualy register the middleware on kernel.php?
I Got, now its working thnks people!!!!
@LeonardoBAV
Could you please give me the sample code?
@praditha
For those still wondering, here is a sample code from @LeonardoBAV 's repositories:
https://github.com/LeonardoBAV/User/blob/9a4054ad8397f116d820c487d0812450250b178f/Providers/UserServiceProvider.php#L30
If you want add a Global Middleware, just instace Kernel Http Contract from Illumite and call pushMiddleware method in your module Service Provider
public function boot(){
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
// adding global middleware
$kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');
$kernel->pushMiddleware('Modules\Frequencia\Http\Middleware\PeriodoMiddleware');
}
public function boot(Illuminate\Routing\Router $router)
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$router->aliasMiddleware('CanReadInvoiceMiddleware', \Modules\Vproducts\Http\Middleware\CanReadInvoiceMiddleware::class);
}
this work fine !!
Most helpful comment
Some changes came into Laravel 5.4. For router middleware you need to use
aliasMiddlewaremethod on$this->app['router']and you are ready to go.