File tree Expand file tree Collapse file tree 5 files changed +87
-19
lines changed Expand file tree Collapse file tree 5 files changed +87
-19
lines changed Original file line number Diff line number Diff line change 31
31
32
32
],
33
33
34
+ /*
35
+ |--------------------------------------------------------------------------
36
+ | Pages
37
+ |--------------------------------------------------------------------------
38
+ |
39
+ | If you want to ensure that the pages exist, you can set `ensure_pages_exist` to true.
40
+ | This will throw an exception if the component does not exist on the filesystem
41
+ | when rendering a page. You may configure this separately for testing.
42
+ |
43
+ */
44
+
45
+ 'ensure_pages_exist ' => false ,
46
+
47
+ 'page_paths ' => [
48
+
49
+ resource_path ('js/Pages ' ),
50
+
51
+ ],
52
+
53
+ 'page_extensions ' => [
54
+
55
+ 'js ' ,
56
+ 'jsx ' ,
57
+ 'svelte ' ,
58
+ 'ts ' ,
59
+ 'tsx ' ,
60
+ 'vue ' ,
61
+
62
+ ],
63
+
34
64
/*
35
65
|--------------------------------------------------------------------------
36
66
| Testing
41
71
| attempts to locate the component as a file relative to any of the
42
72
| paths AND with any of the extensions specified here.
43
73
|
74
+ | By default, it uses the `page_paths` and `page_extensions` settings
75
+ | defined above. You may override these values for testing purposes
76
+ | by adding these two keys to this `testing` array.
77
+ |
44
78
*/
45
79
46
80
'testing ' => [
47
81
48
82
'ensure_pages_exist ' => true ,
49
83
50
- 'page_paths ' => [
51
-
52
- resource_path ('js/Pages ' ),
53
-
54
- ],
55
-
56
- 'page_extensions ' => [
57
-
58
- 'js ' ,
59
- 'jsx ' ,
60
- 'svelte ' ,
61
- 'ts ' ,
62
- 'tsx ' ,
63
- 'vue ' ,
64
-
65
- ],
66
-
67
84
],
68
85
69
86
'history ' => [
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Inertia ;
4
+
5
+ use InvalidArgumentException ;
6
+
7
+ class ComponentNotFoundException extends InvalidArgumentException {}
Original file line number Diff line number Diff line change 11
11
use Illuminate \Support \Facades \Response as BaseResponse ;
12
12
use Illuminate \Support \Traits \Macroable ;
13
13
use Inertia \Support \Header ;
14
+ use InvalidArgumentException ;
14
15
use Symfony \Component \HttpFoundation \RedirectResponse as SymfonyRedirect ;
15
16
use Symfony \Component \HttpFoundation \Response as SymfonyResponse ;
16
17
@@ -156,11 +157,27 @@ public function always($value): AlwaysProp
156
157
return new AlwaysProp ($ value );
157
158
}
158
159
160
+ /**
161
+ * @throws ComponentNotFoundException
162
+ */
163
+ protected function findComponentOrFail (string $ component ): void
164
+ {
165
+ try {
166
+ app ('inertia.view-finder ' )->find ($ component );
167
+ } catch (InvalidArgumentException ) {
168
+ throw new ComponentNotFoundException ("Inertia page component [ {$ component }] not found. " );
169
+ }
170
+ }
171
+
159
172
/**
160
173
* @param array|Arrayable $props
161
174
*/
162
175
public function render (string $ component , $ props = []): Response
163
176
{
177
+ if (config ('inertia.ensure_pages_exist ' , false )) {
178
+ $ this ->findComponentOrFail ($ component );
179
+ }
180
+
164
181
if ($ props instanceof Arrayable) {
165
182
$ props = $ props ->toArray ();
166
183
}
Original file line number Diff line number Diff line change @@ -32,11 +32,19 @@ public function register(): void
32
32
$ this ->registerTestingMacros ();
33
33
$ this ->registerMiddleware ();
34
34
35
+ $ this ->app ->bind ('inertia.view-finder ' , function ($ app ) {
36
+ return new FileViewFinder (
37
+ $ app ['files ' ],
38
+ $ app ['config ' ]->get ('inertia.page_paths ' ),
39
+ $ app ['config ' ]->get ('inertia.page_extensions ' )
40
+ );
41
+ });
42
+
35
43
$ this ->app ->bind ('inertia.testing.view-finder ' , function ($ app ) {
36
44
return new FileViewFinder (
37
45
$ app ['files ' ],
38
- $ app ['config ' ]->get ('inertia.testing.page_paths ' ),
39
- $ app ['config ' ]->get ('inertia.testing.page_extensions ' )
46
+ $ app ['config ' ]->get ('inertia.testing.page_paths ' , fn () => $ app [ ' config ' ]-> get ( ' inertia.page_paths ' ) ),
47
+ $ app ['config ' ]->get ('inertia.testing.page_extensions ' , fn () => $ app [ ' config ' ]-> get ( ' inertia.page_extensions ' ) )
40
48
);
41
49
});
42
50
}
Original file line number Diff line number Diff line change 12
12
use Illuminate \Support \Facades \Request ;
13
13
use Illuminate \Support \Facades \Route ;
14
14
use Inertia \AlwaysProp ;
15
+ use Inertia \ComponentNotFoundException ;
15
16
use Inertia \DeferProp ;
16
17
use Inertia \Inertia ;
17
18
use Inertia \LazyProp ;
@@ -376,4 +377,22 @@ public function toArray()
376
377
],
377
378
]);
378
379
}
380
+
381
+ public function test_will_throw_exception_if_component_does_not_exist_when_ensuring_is_enabled (): void
382
+ {
383
+ config ()->set ('inertia.ensure_pages_exist ' , true );
384
+
385
+ $ this ->expectException (ComponentNotFoundException::class);
386
+ $ this ->expectExceptionMessage ('Inertia page component [foo] not found. ' );
387
+
388
+ (new ResponseFactory )->render ('foo ' );
389
+ }
390
+
391
+ public function test_will_not_throw_exception_if_component_does_not_exist_when_ensuring_is_disabled (): void
392
+ {
393
+ config ()->set ('inertia.ensure_pages_exist ' , false );
394
+
395
+ $ response = (new ResponseFactory )->render ('foo ' );
396
+ $ this ->assertInstanceOf (\Inertia \Response::class, $ response );
397
+ }
379
398
}
You can’t perform that action at this time.
0 commit comments