Skip to content

Commit 6c1bbe7

Browse files
committed
Merge remote-tracking branch 'base/master' into patch-1
2 parents d62a443 + 00100e8 commit 6c1bbe7

File tree

6 files changed

+100
-19
lines changed

6 files changed

+100
-19
lines changed

readme.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,6 @@ If you'd like to use a different root view, you can change it using `Inertia::se
4343
Inertia\Inertia::setRootView('name');
4444
~~~
4545

46-
## Add Inertia middleware
47-
48-
Next, add the `Inertia\Middleware` middleware to your `web` middleware group, found in the `/app/Http/Kernel.php` file. This middleware monitors for asset changes, and also fixes an edge case with 302 redirects. Be sure to include this middleware *after* any session related middleware.
49-
50-
~~~php
51-
protected $middlewareGroups = [
52-
'web' => [
53-
// ...
54-
\Inertia\Middleware::class,
55-
]
56-
];
57-
~~~
58-
5946
## Making Inertia responses
6047

6148
To make an Inertia response, use `Inertia::render()`. This function takes two arguments, the component name, and the component data (props).

src/Controller.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
class Controller
6+
{
7+
public function __invoke($component, $props)
8+
{
9+
return Inertia::render($component, $props);
10+
}
11+
}

src/ServiceProvider.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,38 @@
22

33
namespace Inertia;
44

5+
use Illuminate\Routing\Router;
6+
use Illuminate\Contracts\Http\Kernel;
57
use Illuminate\Support\Facades\Blade;
68
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
79

810
class ServiceProvider extends BaseServiceProvider
911
{
10-
/**
11-
* Perform post-registration booting of services.
12-
*
13-
* @return void
14-
*/
1512
public function boot()
13+
{
14+
$this->registerBladeDirective();
15+
$this->registerRouterMacro();
16+
$this->registerMiddleware();
17+
}
18+
19+
protected function registerBladeDirective()
1620
{
1721
Blade::directive('inertia', function () {
1822
return '<div id="app" data-page="{{ json_encode($page) }}"></div>';
1923
});
2024
}
25+
26+
protected function registerRouterMacro()
27+
{
28+
Router::macro('inertia', function ($uri, $component, $props = []) {
29+
return $this->match(['GET', 'HEAD'], $uri, '\Inertia\Controller')
30+
->defaults('component', $component)
31+
->defaults('props', $props);
32+
});
33+
}
34+
35+
protected function registerMiddleware()
36+
{
37+
$this->app[Kernel::class]->pushMiddleware(Middleware::class);
38+
}
2139
}

tests/ControllerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Inertia\Tests;
4+
5+
use Inertia\Response;
6+
use Inertia\Controller;
7+
use Illuminate\Http\Request;
8+
9+
class ControllerTest extends TestCase
10+
{
11+
public function test_controller_returns_an_inertia_response()
12+
{
13+
$response = (new Controller())('User/Edit', ['user' => ['name' => 'Jonathan']]);
14+
15+
$this->assertInstanceOf(Response::class, $response);
16+
$this->assertEquals([
17+
'page' => [
18+
'component' => 'User/Edit',
19+
'props' => ['user' => ['name' => 'Jonathan']],
20+
'url' => '',
21+
'version' => null,
22+
],
23+
], $response->toResponse(new Request())->getData());
24+
}
25+
}

tests/HelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function test_the_helper_function_returns_an_instance_of_the_response_fac
1515

1616
public function test_the_helper_function_returns_a_response_instance()
1717
{
18-
$this->assertInstanceOf(Response::class, inertia('User/Edit', ['user' => ['name' => 'George']]));
18+
$this->assertInstanceOf(Response::class, inertia('User/Edit', ['user' => ['name' => 'Jonathan']]));
1919
}
2020

2121
public function test_the_instance_is_the_same_as_the_facade_instance()

tests/ServiceProviderTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
use Illuminate\Support\Facades\Blade;
9+
use Illuminate\Support\Facades\Route;
10+
11+
class ServiceProviderTest extends TestCase
12+
{
13+
public function test_blade_directive_is_registered()
14+
{
15+
$directives = Blade::getCustomDirectives();
16+
17+
$this->assertArrayHasKey('inertia', $directives);
18+
$this->assertEquals('<div id="app" data-page="{{ json_encode($page) }}"></div>', $directives['inertia']());
19+
}
20+
21+
public function test_route_macro_is_registered()
22+
{
23+
$route = Route::inertia('/', 'User/Edit', ['user' => ['name' => 'Jonathan']]);
24+
$routes = Route::getRoutes();
25+
26+
$this->assertNotEmpty($routes->getRoutes());
27+
$this->assertEquals($route, $routes->getRoutes()[0]);
28+
$this->assertEquals(['GET', 'HEAD'], $route->methods);
29+
$this->assertEquals('/', $route->uri);
30+
$this->assertEquals(['uses' => '\Inertia\Controller@__invoke', 'controller' => '\Inertia\Controller'], $route->action);
31+
$this->assertEquals(['component' => 'User/Edit', 'props' => ['user' => ['name' => 'Jonathan']]], $route->defaults);
32+
}
33+
34+
public function test_middleware_is_registered()
35+
{
36+
$kernel = App::make(Kernel::class);
37+
38+
$this->assertTrue($kernel->hasMiddleware(Middleware::class));
39+
}
40+
}

0 commit comments

Comments
 (0)