Skip to content

Commit 639f5b7

Browse files
committed
Auto register middleware
1 parent f563b27 commit 639f5b7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/ServiceProvider.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,38 @@
33
namespace Inertia;
44

55
use Illuminate\Routing\Router;
6+
use Illuminate\Contracts\Http\Kernel;
67
use Illuminate\Support\Facades\Blade;
78
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
89

910
class ServiceProvider extends BaseServiceProvider
1011
{
11-
/**
12-
* Perform post-registration booting of services.
13-
*/
1412
public function boot()
13+
{
14+
$this->registerBladeDirective();
15+
$this->registerRouterMacro();
16+
$this->registerMiddleware();
17+
}
18+
19+
protected function registerBladeDirective()
1520
{
1621
Blade::directive('inertia', function () {
1722
return '<div id="app" data-page="{{ json_encode($page) }}"></div>';
1823
});
24+
}
1925

26+
protected function registerRouterMacro()
27+
{
2028
Router::macro('inertia', function ($uri, $component, $props = []) {
2129
return $this->match(['GET', 'HEAD'], $uri, '\Inertia\Controller')
2230
->defaults('component', $component)
2331
->defaults('props', $props);
2432
});
2533
}
34+
35+
protected function registerMiddleware()
36+
{
37+
$kernel = $this->app[Kernel::class];
38+
$kernel->pushMiddleware(Middleware::class);
39+
}
2640
}

tests/ServiceProviderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Inertia\Tests;
4+
5+
use Inertia\Middleware;
6+
use Illuminate\Support\Facades\App;
7+
use Illuminate\Contracts\Http\Kernel;
8+
9+
class ServiceProviderTest extends TestCase
10+
{
11+
public function test_middleware_is_registered()
12+
{
13+
$kernel = App::make(Kernel::class);
14+
15+
$this->assertTrue($kernel->hasMiddleware(Middleware::class));
16+
}
17+
}

0 commit comments

Comments
 (0)