问题
On Laravel 5.2, I'm trying to put two menus (X latest contents, and the same for a specific user) on near every page.
The lazy way would be to generate those arrays in each of the controllers, but is there a way to generate it when the templating system needs them ?
For instance, my template could call something like {{ $menu }}
and the menu would be generated only when this was present ?
回答1:
Laravel has a feature called view composer ,which lets you do some actions when a template file is loaded.
you could set the X latest contents
into a template file and then include it wherever you want to and use view composer to fetch the data.
来源:https://stackoverflow.com/questions/35105578/laravel-having-the-same-element-on-each-of-my-pages