Skip to content

Instantly share code, notes, and snippets.

@danielnv18
Created September 12, 2015 19:38
Show Gist options
  • Save danielnv18/0b83fc81b1deb2089468 to your computer and use it in GitHub Desktop.
Save danielnv18/0b83fc81b1deb2089468 to your computer and use it in GitHub Desktop.
{{ knp_menu_render('main') }}
<?php
namespace AppBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class MenuBuilder
{
private $factory;
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}
public function createMainMenu(RequestStack $requestStack)
{
$menu = $this->factory->createItem('root');
$menu->addChild('level1', ['uri' => '#']);
$menu['level1']->addChild('level2', ['uri' => '#' ]);
$menu['level2']->addChild('level3', ['uri' => '#' ]);
return $menu;
}
}
knp_menu:
# use "twig: false" to disable the Twig extension and the TwigRenderer
twig:
template: knp_menu.html.twig
# if true, enables the helper for PHP templates
templating: false
# the renderer to use, list is also available by default
default_renderer: twig
services:
app.menu_builder:
class: AppBundle\Menu\MenuBuilder
arguments: ["@knp_menu.factory"]
app.main_menu:
class: Knp\Menu\MenuItem # the service definition requires setting the class
factory: ["@app.menu_builder", createMainMenu]
arguments: ["@request_stack"]
tags:
- { name: knp_menu.menu, alias: main } # The alias is what is used to retrieve the menu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment