Kinetic: A view-composer package for Inertia.js

Packages

May 26th, 2022

kinetic-inertia-featured.png

Kinetic adds view-composer-like features to the Inertia.js Laravel adapter. Like Laravel view composers, Kinetic can bind data each time a component is rendered from a single location.

Within a service provider, you can call the composer() method to define Inertia composers:

1// In a service provider
2public function boot()
3{
4 // Class-based composer..
5 Inertia::composer('User/Profile', UserComposer::class);
6}
7 
8// Composer class
9class UserComposer
10{
11 public function compose(ResponseFactory $inertia)
12 {
13 $inertia->with('list', [
14 'foo' => 'bar',
15 'baz' => 'buzz'
16 ]);
17 }
18}

The composer() method supports closure-based composers as well:

1Inertia::composer('User/Profile', function (ResponseFactory $inertia) {
2 $inertia->with([
3 'post' => [
4 'subject' => 'Hello World!',
5 'description' => 'This is a description.'
6 ]
7 ]);
8});

With composers defined in a service provider, your props will include the composing data when you call render():

1// Includes bound data from `Inertia::composer('User/Profile')`
2Inertia::render('User/Profile');

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners