Laravel-modules: Register Middleware

Created on 22 Mar 2017  路  8Comments  路  Source: nWidart/laravel-modules

How is the best way to register some module middeware on the Laravel?

Most helpful comment

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.

All 8 comments

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 !!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uncodable picture uncodable  路  21Comments

cristiangervasi picture cristiangervasi  路  12Comments

elephantux picture elephantux  路  11Comments

ridwanskaterock picture ridwanskaterock  路  12Comments

Alercard picture Alercard  路  29Comments