grudge61's avatar

Dynamic menu

I'm building an e-commerce website and I wondering how to list categories in my top menu without passing data, on every method, to view.

Someone have any idea how to do it?

0 likes
3 replies
ekaitzastiz's avatar
Level 9

I am not sure if you are talking about getting menu items every method in controller and passing to the view. If it's that, use VIEW COMPOSERS, you can find the documentation here.

you can do a partial for menu and bind the composer to that view, or you can share that data through any view. I would use the first option for this case:

http://laravel.com/docs/responses#view-composers

View::composer('_includes.menu',function($view){
$menuItems = ['users','stats','tags'];
$view->with(compact('menuItems');
})

or do it in a class:

View::composer('_includes.menu', 'Menuapp\Composers\MenuComposer');

and do what you need in your class.

You can put this "view::composer()" in routes.php, or if you prefer in a composer.php and define it to your path app/start/global.php file.

1 like

Please or to participate in this conversation.