Skip to content

Commit b2e5729

Browse files
committed
add inertia render hooks
1 parent 700c635 commit b2e5729

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/Http/Controllers/Inertia/TeamController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function show(Request $request, $teamId)
2929
abort(403);
3030
}
3131

32-
return Inertia::render('Teams/Show', [
32+
return Jetstream::inertia()->render($request, 'Teams/Show', [
3333
'team' => $team->load('owner', 'users'),
3434
'availableRoles' => array_values(Jetstream::$roles),
3535
'availablePermissions' => Jetstream::$permissions,

src/InertiaManager.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Laravel\Jetstream;
4+
5+
use Illuminate\Http\Request;
6+
use Inertia\Inertia;
7+
8+
class InertiaManager
9+
{
10+
/**
11+
* The registered rendering callbacks.
12+
*
13+
* @var array
14+
*/
15+
protected $renderingCallbacks = [];
16+
17+
/**
18+
* Render the given Inertia page.
19+
*
20+
* @param \Illuminate\Http\Request $request
21+
* @param string $page
22+
* @param array $data
23+
* @return \Inertia\Response
24+
*/
25+
public function render(Request $request, string $page, array $data = [])
26+
{
27+
if (isset($this->renderingCallbacks[$page])) {
28+
foreach ($this->renderingCallbacks[$page] as $callback) {
29+
$data = $callback($request, $data);
30+
}
31+
}
32+
33+
return Inertia::render($page, $data);
34+
}
35+
36+
/**
37+
* Register a rendering callback.
38+
*
39+
* @param string $page
40+
* @param callable $callback
41+
* @return $this
42+
*/
43+
public function whenRendering(string $page, callable $callback)
44+
{
45+
$this->renderingCallbacks[$page][] = $callback;
46+
47+
return $this;
48+
}
49+
}

src/Jetstream.php

+21
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class Jetstream
5959
*/
6060
public static $membershipModel = 'App\\Models\\Membership';
6161

62+
/**
63+
* The Inertia manager instance.
64+
*
65+
* @var \Laravel\Jetstream\InertiaManager
66+
*/
67+
public static $inertiaManager;
68+
6269
/**
6370
* Determine if Jetstream has registered roles.
6471
*
@@ -347,6 +354,20 @@ public static function deleteUsersUsing(string $class)
347354
return app()->singleton(DeletesUsers::class, $class);
348355
}
349356

357+
/**
358+
* Manage Jetstream's Inertia settings.
359+
*
360+
* @return \Laravel\Jetstream\InertiaManager
361+
*/
362+
public static function inertia()
363+
{
364+
if (is_null(static::$inertiaManager)) {
365+
static::$inertiaManager = new InertiaManager;
366+
}
367+
368+
return static::$inertiaManager;
369+
}
370+
350371
/**
351372
* Configure Jetstream to not register its routes.
352373
*

0 commit comments

Comments
 (0)