What about this?
Inertia - shared data on certain pages
Does anyone have any great ideas on how to add shared data to certain pages only? Something like view composers, but for inertia..
https://inertiajs.com/shared-data
My best idea so far is to have a check on each share with something like if($request->is('admin')) but I fear it will get messy fast. I have considered if it would be possible to split it into two middlewares, but I cant see how..
I have read this discussion, but was hoping for something closer to view composers :) https://github.com/inertiajs/inertia-laravel/issues/74
@bugsysha Sadly that just shows how to use shared data for all pages, which I am already doing. My problem is limiting which pages it is actually shown on (like a view composer)
https://laravel.com/docs/8.x/views#attaching-a-composer-to-multiple-views
@Sinnbeck then my memory tricked me that it was covered in that video. Sorry. Can you explain what is the problem if you have the data on all pages?
@bugsysha I have created an intranet where the whole site is built on inertia. Every page has the same menu where I have shared data like the menu structure.. Not the problem is that I have made a few other types of pages, that is not using the same layout/menu, and therefor does not need any of that stuff. So I am trying to find an elegant way of ensuring that those pieces of shared data isnt used.
@Sinnbeck there is nothing from stopping you to control it from that middleware as it was shown in the video. Just make it a bit dynamic so it does not get messy.
@bugsysha Yeah I have been considering using an array, like with the one in CSRF, to tell it which routes to not add the shared data to. I was just hoping for a cleaner solution :)
@Sinnbeck would something like this clean things up?
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
$methodName = $this->convertCurrentRouteToMethodName($request);
return array_merge(parent::share($request), $this->$methodName());
}
}
Ofc, you can always do the same with classes. Just make classes invokable.
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
$className = $this->convertCurrentRouteToClassName($request);
return array_merge(parent::share($request), new $className);
}
}
@bugsysha Currently I am trying to make some sort of clever macro to allow me to make my own "view" composer :)
Apparently it works if I extract the shared data into a new middleware (in the handle method), which I then add to the routes that are are using the layout :) Easy
Hi, @Sinnbeck could you show us how you were able to accomplish this with some examples? Thank you
@psalcedodev I just made a middleware class with the needed shared data
public function handle(Request $request, Closure $next)
{
Inertia::share([
'menu' => function () {
return (new Menu)->main();
},
]);
return $next($request);
}
and used it on a route group
Route::middleware([
SharedMenu::class, //this is the middleware class
'auth',
])->group(function () {
@Sinnbeck Thanks for this idea/suggestion. I was searching around for a better solution than using the default Intertia middleware.
What I was contemplating doing is making use of Pinia, Axios and hitting an API endpoint that way I could use that specific data in whatever component I wanted and not touch Inertia's middleware.
Unfortunately, that led me down a rabbit hole, taking way to much time and resources to implement. I do hope they find a better way to share data besides props drilling or provide/inject.
@Sinnbeck just curious, why not a different layout customized for those pages?
@jlrdw they do have different layouts. But each layout needs different data from laravel. In my case I have an admin panel that needs a menu structure based on permissions, and a chat which needs a user list and unread message counts. Both have different layouts but need completely different data from laravel.
@jd45 glad it could help. Yeah doing it yourself would probably be tricky and go against inertia. You can try checking out kinetic. It's view composers for inertia
@Sinnbeck Thank you! Looks like it is exactly what I need!
Please or to participate in this conversation.