Kinetic: A view-composer package for Inertia.js

Packages

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:

// In a service provider
public function boot()
{
// Class-based composer..
Inertia::composer('User/Profile', UserComposer::class);
}
 
// Composer class
class UserComposer
{
public function compose(ResponseFactory $inertia)
{
$inertia->with('list', [
'foo' => 'bar',
'baz' => 'buzz'
]);
}
}

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

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

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

// Includes bound data from `Inertia::composer('User/Profile')`
Inertia::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.