Laravel-modules: How can i define new Middleware in module

Created on 26 Dec 2017  路  6Comments  路  Source: nWidart/laravel-modules

Most helpful comment

In case anyone is still confused, in your module's service provider's boot method, add register your middleware thus
app()->make('router')->aliasMiddleware('auth2', \Your\Namespace\Class\Auth2::class);

This assumes you are trying to create a middleware called auth2. Then you can use it on your controllers or routes like:

Route::middleware('auth2') ....

$this->middleware('auth2') ....

All 6 comments

You can register your middleware inside your module service provider using this->middleware() method. Or add it in the Kernel.php of the app/Http directory.

Could you provide me the sample please?

@nWidart that no longer works, you must use ->aliasMiddleware()

Call to undefined method Modules\Vproducts\Providers\VproductsServiceProvider::middleware()

In case anyone is still confused, in your module's service provider's boot method, add register your middleware thus
app()->make('router')->aliasMiddleware('auth2', \Your\Namespace\Class\Auth2::class);

This assumes you are trying to create a middleware called auth2. Then you can use it on your controllers or routes like:

Route::middleware('auth2') ....

$this->middleware('auth2') ....

Find module service provider file under Module->Providers->ModuleServiceProvider.php, Append middleware in boot method.

Exa.
InstallerServiceProvider.php

public function boot(Illuminate\Routing\Router $router)
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));

   // solution 1
    $router->middlewareGroup('update', [\Modules\Installer\Http\Middleware\CanUpdate::class]);

    // solution 2
    app()->make('router')->aliasMiddleware('update', \Modules\Installer\Http\Middleware\CanUpdate::class);

}

This work fine!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oleaass picture oleaass  路  11Comments

bhattraideb picture bhattraideb  路  12Comments

KyawNaingTun picture KyawNaingTun  路  16Comments

echemik picture echemik  路  10Comments

bashet picture bashet  路  19Comments