Skip to content

Commit 636178b

Browse files
committed
feat: add shared props docs
1 parent 1ec1778 commit 636178b

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/docs/frontend/inertia.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ php leaf view:install --svelte
2424

2525
Adding Inertia to your Leaf app doesn't change the way you handle routing, so you'll still be using your controllers, except that instead of a Blade view, you would return an inertia view:
2626

27-
```php [app/controllers/MyController.php]
27+
```php:no-line-numbers [app/controllers/MyController.php]
2828
return response()->view('something'); // [!code --]
2929
return response()->inertia('something'); // [!code ++]
3030
```
3131

3232
If you need to return a view directly without the need for a controller, you can add an inertia route directly like this:
3333

34-
```php [app/routes/_route.php]
34+
```php:no-line-numbers [app/routes/_route.php]
3535
app()->inertia('/some-route', 'something');
3636
```
3737

@@ -123,6 +123,22 @@ defineProps({ prop1: Array, ... });
123123

124124
You can find more information on using Inertia with your frontend framework in the [Inertia documentation](https://inertiajs.com/).
125125

126+
## Shared Data <Badge text="New" type="tip" />
127+
128+
Sometimes, you might want to share data across all your inertia views. Leaf MVC makes this super easy by providing a simple way to do this. You can use the `Inertia::share()` method to share data across all your inertia views. You can do this in your `app/routes/index.php` file like this:
129+
130+
```php:no-line-numbers [app/routes/index.php]
131+
use Leaf\Inertia;
132+
133+
Inertia::share('appName', 'Some constant value');
134+
Inertia::share('someDeferredValue', fn() => asyncData()->get() ?? null);
135+
Inertia::share('specialFlashMessage', function () {
136+
return flash()->display('specialFlashMessage') ?? null;
137+
});
138+
```
139+
140+
Using a function to share data is useful when you want to share dynamic data, because the function won't be executed until the data is actually needed, so if you share something like a flash message which can only be read once, it won't be lost.
141+
126142
## Generating Inertia Views
127143

128144
Once you set up your preferred frontend framework using the `view:install` command, Leaf MVC automatically reconfigures the framework to work primarily with your tooling. So you can generate a new inertia view using the `g:template` command.

0 commit comments

Comments
 (0)