diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6b17041f..20c3e167 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -53,12 +53,15 @@ protected function registerRouterMacro() protected function registerMiddleware() { $kernel = $this->app[Kernel::class]; + $group = Config::get('inertia.middleware_group', 'web'); + // Laravel >= 6.9.0 if (method_exists($kernel, 'appendMiddlewareToGroup')) { - $kernel->appendMiddlewareToGroup( - Config::get('inertia.middleware_group', 'web'), - Middleware::class - ); + $kernel->appendMiddlewareToGroup($group, Middleware::class); + + // Laravel >= 5.4.4 && < 6.9.0 + } elseif ($this->app[Router::class]->hasMiddlewareGroup($group)) { + $this->app[Router::class]->pushMiddlewareToGroup($group, Middleware::class); } } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index fa6905bc..d5120fe3 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -6,6 +6,7 @@ use Inertia\Inertia; use Inertia\Middleware; use Illuminate\Http\Request; +use Illuminate\Routing\Router; use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\App; use Illuminate\Support\ViewErrorBag; @@ -48,11 +49,16 @@ public function test_route_macro_is_registered() $this->assertEquals(['component' => 'User/Edit', 'props' => ['user' => ['name' => 'Jonathan']]], $route->defaults); } - public function test_middleware_is_registered() + public function test_middleware_is_registered_to_the_web_group() { - $middleware = App::make(Kernel::class)->getMiddlewareGroups(); + $webRoute = Route::middleware('web')->get('/'); + $apiRoute = Route::middleware('api')->get('/'); - $this->assertContains(Middleware::class, $middleware['web']); + $webMiddleware = App::make(Router::class)->gatherRouteMiddleware($webRoute); + $apiMiddleware = App::make(Router::class)->gatherRouteMiddleware($apiRoute); + + $this->assertContains(Middleware::class, $webMiddleware); + $this->assertNotContains(Middleware::class, $apiMiddleware); } public function test_validation_errors_are_registered()