Assign a key to middleware in package development

Published 1 week ago by Tetravalence

Hello, I'd like to share Middlewares across a group of routes without needing to use their FQN. I know it's possible to assign the Middleware a key in app/Http/Kernel.php but I'm looking for a solution in package development that does not require to change the aforementioned file. Right now I'm using the MyPackageDashboard Middleware by means of his FQN

Route::middleware('auth', Tetravalence\MyPackage\Http\Middleware\MyPackageDashboard::class)->group(function () {
...
// group of routes
});

Where can I set up these keys in package development?

Best Answer (As Selected By Tetravalence)
tykus

In your service provider, register a :

$this->app['router']->aliasMiddleware(
    'your-alias', // whatever name your preferred name
    Tetravalence\MyPackage\Http\Middleware\MyPackageDashboard::class
);
tykus
tykus
1 week ago (620,710 XP)

In your service provider, register a :

$this->app['router']->aliasMiddleware(
    'your-alias', // whatever name your preferred name
    Tetravalence\MyPackage\Http\Middleware\MyPackageDashboard::class
);
Tetravalence

@tykus OK. Thank you!

Please sign in or create an account to participate in this conversation.