#Is this good practice?

26 messages · Page 1 of 1 (latest)

sly hornet
#

So I was trying to make it so that every component has access to the name, but if I put this function in every controller, it would be very redundant. I did a bit of "research," and it recommends I change the share method of HandleInertiaRequests (which is a prebuilt middleware). I do not know if this is the best way.

peak merlin
#

Yes, if you need to share data globally, to every single page or for example a layout, you’d use the middleware to share data. Sharing user details is a typical thing to do

sly hornet
peak merlin
sly hornet
peak merlin
sly hornet
sly hornet
#

@peak merlin Am I doing this correctly?

#

oh wait

#

I didn't use share

#

Aight fixed it

#

wait

peak merlin
#

I still don’t get why you need a separate middleware for that. Maybe it didn’t come across well, but with my question I tried to make you think about how it works and what the benefits would be. So, no, I wouldn’t use a separate middleware, the Inertia middleware is built to share global data, and from what I can tell, you don’t have a need for another middleware

sly hornet
#

No need controller

sly hornet
peak merlin
sly hornet
#

Also I realized that they are basically the same thing lol

sly hornet
#

@peak merlin SInce usercount is only needed for my dashboard route, I grabbed the data from the controller. Is this the right way, right?

peak merlin
#

yes.

golden helm
sly hornet
#

Thanks guys

remote galleon
#

I recently came across a case where three different routes needed access to some of the same data, and rendered the same React component in a sidebar, but those routes (and the associated data) were only accessible by users with a certain role so sharing the data globally made no sense.

I went with sharing using Inertia::share() in a middleware on the route group, and putting the relevant sidebar in a layout component used by the relevant page components.

I am not convinced it is the right solution now, as I am a little worried about indirection. I think just duplicating a couple of lines of code in each of the route handlers might be clearer.

In any case I am glad I explored the possibility. It is quite neat really. It feels similar to the route loader / outlet pattern in Remix / RRv6. I just wish the API was a little more self-evident so that when someone else reads it in 6 months they'd stand a better chance of immediately groking where the data is coming from.