Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - Generate menu entries from available bundles

I'm new to web development with Symphony2 (though definitely not new to web development), and I'm just about to begin a medium sized project, which will be sliced in bundles, as each installation of the app may have a different setup of available functionality.

I would like to generate my navigation dynamically from the available bundles, e.g. if the bundle "foo" is active, a menu entry with a route to the foo main controller action should appear.

Normally, my take on this would be to create a singleton somewhere, which I then would fill during the load() function of a bundle, and during rendering, I would output the singleton.

But symfony2 offers a lot flexibility at this part, so I'm currently evaluating if there may be a better way.

Could services be a way to go here? Or events? Or something with dependency injection, so the bundles get an instance of a NavigationConfigurationElement at construction time?

Any input or thoughts on this, or maybe some links to examples how to do this, would be greatly appreciated.

Best regards, Jens

like image 581
jhoffrichter Avatar asked May 29 '12 14:05

jhoffrichter


1 Answers

i thing the best way to do it, is to use dependency injections tags. you will have to create a dependency injection extension and offer a "tag" that can be used by the various bundles to register their menu entries.

i will not describe you the whole process here because there is plenty of resources about that in the internet.

but to give you a quick outline of what to do

  1. implement a service holding the menu entries (the singleton you where talking about)
  2. process the tag by implementing a compiler pass, this compiler pass will look for all services tagged with the navigation class and register them with the menu service
  3. create a twig function that will use the service to retrieve the menu and render it
  4. write bundles that use the tag and provide menu items

here are some resources that might help you:

http://symfony.com/doc/current/components/dependency_injection/tags.html http://miguel.ibero.me/es/post/2012-04-28/adding-tags-to-symfony.html

i'm currently implementing a solr bundle for symfony that uses DI tags as well. i have a class called IndexManager that manages various solr indexes from different bundles. i use the DI tag so other bundles can register content/entities they want to be indexed in solr. the principle is the same as with the menu items.

see here: https://github.com/roomthirteen/Room13SolrBundle

the important files are:

adding the compiler pass: https://github.com/roomthirteen/Room13SolrBundle/blob/master/Room13SolrBundle.php the compiler pass itself: https://github.com/roomthirteen/Room13SolrBundle/blob/master/DependencyInjection/Compiler/SolrCompilerPass.php

hope that helps. any more questins? don't hesitate to ask.

like image 193
room13 Avatar answered Sep 22 '22 06:09

room13