Skip to content

Commit

Permalink
Merge pull request #153 from claudiodekker/laravel-5.4-auto-middlewar…
Browse files Browse the repository at this point in the history
…e-registration

Support Auto Registration of Middleware on Laravel 5.4+
  • Loading branch information
reinink authored Sep 30, 2020
2 parents fecb9ec + 1e4ae86 commit 2cee543
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 9 additions & 3 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2cee543

Please sign in to comment.