Best way to get some data on each request and pass to view


#1

Let’s say we have menu items that are kept in database, these items are displayed on each page, what is the best way getting them and passing to view?


#2

I thought about using a View hook registered after.providersBooted and querying menu items inside, then generating markup and passing, but this will only request items once.
Is there a way better?


#3

You want to fetch the items on each page reload?


#4

Basically yes, because these menu items may be added or edited in management area.
Currently I did it just querying menus in each controller and passing data to view, but this is not really very DRY, so I decided to ask for a better solution…


#5

Then, I suggest using the middleware for that.

async handle ({ view }) {
  const menuItems = // get them from somewhere
  view.share({ menuItems })
}