20

I'm currently developing a package in/for Laravel 5.
My package contains a custom middleware and I would like to add it to the $routeMiddlewarearray of the Kernel class from in my package Service Provider.
But I can't seem to find a way to do this.

I tried to make a custom class that extends the Kernel class and then I can merge the array with my array.
But once out of the constructor it's not possible.

In L4 you had App::middleware, but that function is no longer available in L5.

Can anyone who has solved this problem help me solving this?

Please, tell me if my question is not clear enough, so that I can clarerify it a bit.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Joren Van Hocht
  • 845
  • 1
  • 9
  • 21

7 Answers7

31

Since Laravel 5.4 (tested up to 5.8) you would call the following line from a service provider.

$this->app['router']->aliasMiddleware('my-package-middleware', \My\Package\Middleware::class);

Or you can use the app() helper as below.

app('router')->aliasMiddleware('my-package-middleware', \My\Package\Middleware::class);
mitchdav
  • 695
  • 8
  • 12
17

It's about Laravel 5.6

/*In your package service provider*/
public function boot()
{
    /** @var Router $router */
    $router = $this->app['router'];
    $router->pushMiddlewareToGroup('web', MyPackage\Middleware\WebOne::class);
}
  • Adding to a new group causes "Target class [my_middleware] does not exist" while "aliasMiddleware" works fine – ymakux Feb 03 '20 at 10:04
13

In your package service provider you can access the router instance like this:

$this->app['router']

Next you can register middleware like this:

$this->app['router']->middleware('middlewareName', 'your\namespace\MiddlewareClass');

you place this code in the register method of your service provider.

Joren Van Hocht
  • 845
  • 1
  • 9
  • 21
0

You can do this with these two ways:

  • Register your middle-ware inside your general middle-ware provider
  • Register the middle-ware inside your package Service Provider

lets try first one by create your TestMiddleware.php file in your src package folder and place it in somewhere, in my case i placed it inside Middle-ware folder and then add it to your composer.json auto-loader like this:

"autoload": {

  "psr-4": {
            "Vendor\\Package\\Middleware": "packages/Vendor/Package/src/Middleware"
   }
}

And write your general middle-ware:

namespace Vendor\Package\Middleware;

class TestMiddleware {

    public function handle( $request, Closure $next ) {
        echo 'hello, world!';
    }

}

And then then add the Middle-ware to your main project middle-ware, in Lumen you should add it like this:

 $app->middleware([
    Vendor\Package\Middleware\TestMiddleware::class
 ]);

Add middle-ware to package service provider

And the second way, create middle-ware and load it in autoloader like last example and then create a service provider and register your middle-ware inside boot method:

public function boot()
{
        $this->app->middleware([
               \Vendor\Package\Middleware\TestMiddleware::class
        ]);
}

And finally you should register your service provider inside your main project (Lumen example)

  $app->register(Vendor\Package\TestServiceProvider::class);
Mehdi Hosseini
  • 1,937
  • 1
  • 18
  • 29
0

in laravel 9 in package service provider and in register function will use app('router')->aliasMiddleware method for ex:

app('router')->aliasMiddleware('yourmiddleware',yourclass::class)
-1

Solution for Lumen:

$this->app->middleware($middleware);
Ostap Brehin
  • 3,240
  • 3
  • 25
  • 28
-3

There are two ways to register your package middlewares.

The first way would be pushing middleware into existing middleware group with the following method:

$this->middlewareGroup($group_name, $middleware_list)

And the other way would be registering a custom middleware and assigning your middlewares to that group.

$this->pushMiddlewareToGroup($group_name, $middleware_list)

You can learn more in the links below

middlewareGroup:
https://laravel.com/api/5.5/Illuminate/Routing/Router.html#method_middlewareGroup

pushMiddlewareToGroup: https://laravel.com/api/5.5/Illuminate/Routing/Router.html#method_pushMiddlewareToGroup

Agil
  • 376
  • 5
  • 26
  • 2
    You seem to have it the other way around. `middlewareGroup` is for creating a group and `pushMiddlewareToGroup` is for pushing to an existing group. – Martin Oct 06 '19 at 22:28
  • @Martin isn't that what I mentioned in my answer already? Please read carefully the entire answer, not just the code lines – Agil Nov 19 '19 at 15:11
  • The method in your first line of code is for registering a group of middleware, though it says `pushing middleware into existing middleware group` right above it. – Martin Nov 20 '19 at 04:28