, (*1)
Este bundle Ć© responsavel por Customizar a criaĆ§Ć£o de menus do KnpMenuBundle, (*2)
InstalaĆ§Ć£o
Para instalar o bundle basta rodar o seguinte comando abaixo:, (*3)
$ composer require baconmanager/menu-bundle
Agora adicione os seguintes bundles no arquivo AppKernel.php:, (*4)
<?php
// app/AppKernel.php
public function registerBundles()
{
// ...
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new Bacon\Bundle\MenuBundle\BaconMenuBundle(),
// ...
}
ConfiguraĆ§Ć£o
Adicionar as configuraƧƵes abaixo no arquivo app/config/config.yml, (*5)
#Menu
knp_menu:
# use "twig: false" to disable the Twig extension and the TwigRenderer
twig:
template: BaconCoreBundle:partial: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
Criando um menu
Para criar um novo menu basta criar uma classe no namespace MyBundle/Menu/Builder como no exemplo abaixo:, (*6)
Para versĆ£o symfony =< 2.7, (*7)
<?php
// src/AppBundle/Menu/Builder.php
namespace AppBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class Builder extends ContainerAware
{
public function addMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$translate = $this->container->get('translator');
// Menu Catalog
$menu->addChild($translate->trans('Category'))->setAttribute('icon', '<i class="fa fa-book"></i>');
$menu[$translate->trans('Category')]->addChild($translate->trans('List'),array('route' => 'admin_category'));
$menu[$translate->trans('Category')]->addChild($translate->trans('New'),array('route' => 'admin_category_new'));
return $menu;
}
}
Para versĆ£o do symfony >= 2.8, (*8)
<?php
// src/AppBundle/Menu/Builder.php
namespace AppBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class Builder implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function addMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$translate = $this->container->get('translator');
// Menu Catalog
$menu->addChild($translate->trans('Category'))->setAttribute('icon', '<i class="fa fa-book"></i>');
$menu[$translate->trans('Category')]->addChild($translate->trans('List'),array('route' => 'admin_category'));
$menu[$translate->trans('Category')]->addChild($translate->trans('New'),array('route' => 'admin_category_new'));
return $menu;
}
}
Renderizando o menu no layout(twig):
{{ bacon_menu_full_render() }}