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 29
29
30
30
],
31
31
32
+ /*
33
+ |--------------------------------------------------------------------------
34
+ | Pages
35
+ |--------------------------------------------------------------------------
36
+ |
37
+ | If you want to ensure that the pages exist, you can set `ensure_pages_exist` to true.
38
+ | This will throw an exception if the component does not exist on the filesystem
39
+ | when rendering a page. You may configure this separately for testing.
40
+ |
41
+ */
42
+
43
+ 'ensure_pages_exist ' => false ,
44
+
45
+ 'page_paths ' => [
46
+
47
+ resource_path ('js/Pages ' ),
48
+
49
+ ],
50
+
51
+ 'page_extensions ' => [
52
+
53
+ 'js ' ,
54
+ 'jsx ' ,
55
+ 'svelte ' ,
56
+ 'ts ' ,
57
+ 'tsx ' ,
58
+ 'vue ' ,
59
+
60
+ ],
61
+
32
62
/*
33
63
|--------------------------------------------------------------------------
34
64
| Testing
39
69
| attempts to locate the component as a file relative to any of the
40
70
| paths AND with any of the extensions specified here.
41
71
|
72
+ | By default, it uses the `page_paths` and `page_extensions` settings
73
+ | defined above. You may override these values for testing purposes
74
+ | by adding these two keys to this `testing` array.
75
+ |
42
76
*/
43
77
44
78
'testing ' => [
45
79
46
80
'ensure_pages_exist ' => true ,
47
81
48
- 'page_paths ' => [
49
-
50
- resource_path ('js/Pages ' ),
51
-
52
- ],
53
-
54
- 'page_extensions ' => [
55
-
56
- 'js ' ,
57
- 'jsx ' ,
58
- 'svelte ' ,
59
- 'ts ' ,
60
- 'tsx ' ,
61
- 'vue ' ,
62
-
63
- ],
64
-
65
82
],
66
83
67
84
'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
@@ -158,11 +159,27 @@ public function always($value): AlwaysProp
158
159
return new AlwaysProp ($ value );
159
160
}
160
161
162
+ /**
163
+ * @throws ComponentNotFoundException
164
+ */
165
+ protected function findComponentOrFail (string $ component ): void
166
+ {
167
+ try {
168
+ app ('inertia.view-finder ' )->find ($ component );
169
+ } catch (InvalidArgumentException ) {
170
+ throw new ComponentNotFoundException ("Inertia page component [ {$ component }] not found. " );
171
+ }
172
+ }
173
+
161
174
/**
162
175
* @param array|Arrayable $props
163
176
*/
164
177
public function render (string $ component , $ props = []): Response
165
178
{
179
+ if (config ('inertia.ensure_pages_exist ' , false )) {
180
+ $ this ->findComponentOrFail ($ component );
181
+ }
182
+
166
183
if ($ props instanceof Arrayable) {
167
184
$ props = $ props ->toArray ();
168
185
}
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