-
Notifications
You must be signed in to change notification settings - Fork 265
[2.x] Introduce ProvidesInertiaProp
interface
#746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Marked as draft as I have doubts about the |
InertiaResponsable
interfaceProvidesInertiaProp
interface
Renamed it to |
Would be sweet if we could have public function getProps(DonationFlowSession $session): array
{
return [
'prop_one' => 'value',
'prop_two' => Inertia::defer(fn() => 'value2'),
];
} And we unpack these using the $handler = new MyHandlerClass($var);
return Inertia::render('component', [
'user' => auth()->user(),
...$handler->getProps()
]); But would be interesting to be able to pass: $handler = new MyHandlerClass($var);
return Inertia::render('component', [
'user' => auth()->user(),
$handler
]); Or even something like: return Inertia::render('component', [
'user' => auth()->user(),
])->with($handler); All the goodies from your use case would be provided - the |
Thanks @danmatthews, that's a very interesting idea! I'm not sure if we could reuse this new interface for it, but it makes me wonder if I should rename it to |
@danmatthews I've opened a PR for this #748 |
Renamed |
This PR introduces an
ProvidesInertiaProp
interface. It acts more or less the same as Laravel'sIlluminate\Contracts\Support\Responsable
interface, but tailored to Inertia.The interface only has one method:
toInertiaProp(PropContext $prop)
. ThePropContext
class contains info about the key, (partly) transformed props array, andRequest
instance. I opted for thePropContext
class instead of passing arguments to keep the method signature easy and clean, and so that we could extend/improvePropContext
later on without breaking the interface.There are several use-cases where it would be nice if you had a little more info than just the
Request
instance:Using the key
In PR #744, there is a request to merge data from the controller into shared data. Currently, you'd do something like this:
There's currently no easy way to implement custom prop types. You could add a macro, but that won't help you much. With the new
ProvidesInertiaProp
, you could introduce aMergeWithSharedProp
class in your own codebase:This would lead to a much cleaner controller.
When combined with macros, you could end up with
Inertia::mergeWithShared(['additional'])
, which is quite nice!Using the props
There are use-cases when you want to know about the other props when transforming one. I've experienced this firsthand in my Table package. If you use two tables on a page, you need to prefix the query params for things like pagination. Therefore, I now have this solution where you can name your tables:
If the table classes would implement
ProvidesInertiaProp
, I could inspect whether there are other table instances in the props, and automatically set the names.