Kinetic: A view-composer package for Inertia.js


May 26th, 2022

Kinetic: A view-composer package for Inertia.js

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()
4 // Class-based composer..
5 Inertia::composer('User/Profile', UserComposer::class);
8// Composer class
9class UserComposer
11 public function compose(ResponseFactory $inertia)
12 {
13 $inertia->with('list', [
14 'foo' => 'bar',
15 'baz' => 'buzz'
16 ]);
17 }

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 ]);

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')`

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.