recipe_knp_menu.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. KnpMenu
  2. =======
  3. The admin comes with `KnpMenu`_ integration.
  4. It integrates a menu with the KnpMenu library. This menu can be a SonataAdmin service, a menu created with a Knp menu provider or a route of a custom controller.
  5. Add a custom controller entry in the menu
  6. -----------------------------------------
  7. To add a custom controller entry in the admin menu:
  8. Create your controller:
  9. .. code-block:: php
  10. class BlogController
  11. {
  12. /**
  13. * @Route("/blog", name="blog_home")
  14. */
  15. public function blogAction()
  16. {
  17. // ...
  18. }
  19. /**
  20. * @Route("/blog/article/{articleId}", name="blog_article")
  21. */
  22. public function ArticleAction($articleId)
  23. {
  24. // ...
  25. }
  26. }
  27. Add the controller route as an item of the menu:
  28. .. code-block:: yaml
  29. # Default configuration for "SonataAdminBundle"
  30. sonata_admin:
  31. dashboard:
  32. groups:
  33. news:
  34. label: ~
  35. label_catalogue: ~
  36. items:
  37. - sonata.news.admin.post
  38. - route: blog_home
  39. label: Blog
  40. - route: blog_article
  41. route_params: { articleId: 3 }
  42. label: Article
  43. ...
  44. You can also override the template of knp_menu used by sonata. The default one is `SonataAdminBundle:Menu:sonata_menu.html.twig`:
  45. .. code-block:: yaml
  46. # Default configuration for "SonataAdminBundle"
  47. sonata_admin:
  48. templates:
  49. knp_menu_template: ApplicationAdminBundle:Menu:custom_knp_menu.html.twig
  50. ...
  51. And voilà, now you have a menu group which contains a link to a sonata admin via its id, to your blog and to a specific article.
  52. Using a menu provider
  53. ---------------------
  54. As seen above, the main way to declare your menu is by declaring items in your sonata admin config file. In some case you may have to create a more complex menu depending on your business logic. This is possible by using a menu provider to populate a whole menu group. This is done with the ``provider`` config value.
  55. The following configuration uses a menu provider to populate the menu group ``my_group``:
  56. .. code-block:: yaml
  57. sonata_admin:
  58. dashboard:
  59. groups:
  60. my_group:
  61. provider: 'MyBundle:MyMenuProvider:getMyMenu'
  62. icon: '<i class="fa fa-edit"></i>'
  63. With KnpMenuBundle you can create a custom menu by using a builder class or by declaring it as a service. Please see the `Knp documentation`_ for further information.
  64. In sonata, whatever the implementation you choose, you only have to provide the menu alias to the provider config key:
  65. * If you are using a builder class, your menu alias should be something like ``MyBundle:MyMenuProvider:getMyMenu``.
  66. * If you are using a service, your menu alias is the alias set in the ``knp_menu.menu`` tag. In the following example this is ``my_menu_alias``:
  67. .. code-block:: xml
  68. <service id="my_menu_provider" class="MyBundle/MyDirectory/MyMenuProvider">
  69. <tag name="knp_menu.menu" alias="my_menu_alias" />
  70. </service>
  71. Please note that when using the provider option, you can't set the menu label via the configuration. It is done in your custom menu.
  72. Extending the menu
  73. ------------------
  74. You can modify the menu via events easily. You can register as many listeners as you want for the event with name ``sonata.admin.event.configure.menu.sidebar``:
  75. .. code-block:: php
  76. // src/AppBundle/EventListener/MenuBuilderListener.php
  77. namespace AppBundle\EventListener;
  78. use Sonata\AdminBundle\Event\ConfigureMenuEvent;
  79. class MenuBuilderListener
  80. {
  81. public function addMenuItems(ConfigureMenuEvent $event)
  82. {
  83. $menu = $event->getMenu();
  84. $menu->addChild('reports', array(
  85. 'route' => 'app_reports_index',
  86. 'labelAttributes' => array('icon' => 'glyphicon glyphicon-stats'),
  87. ))->setLabel('Daily and monthly reports');
  88. }
  89. }
  90. .. code-block:: yaml
  91. services:
  92. app.menu_listener:
  93. class: AppBundle\EventListener\MenuBuilderListener
  94. tags:
  95. - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }
  96. Please see the `Using events to allow a menu to be extended`_ for further information.
  97. .. _KnpMenu: https://github.com/KnpLabs/KnpMenu
  98. .. _Knp documentation: http://symfony.com/doc/current/bundles/KnpMenuBundle/index.html#create-your-first-menu
  99. .. _Using events to allow a menu to be extended: http://symfony.com/doc/master/bundles/KnpMenuBundle/events.html