在没有太多开销的情况下实现这一点的唯一方法是使用KnpLabs/KnpMenuBundle。
在您的 global.html.twig 中:
<ul class="nav">
{{ knp_menu_render('YourModule1Bundle:MenuBuilder:module1Menu', {'currentClass': 'active'}) }}
{{ knp_menu_render('YourModule2Bundle:MenuBuilder:module2Menu', {'currentClass': 'active'}) }}
{{ knp_menu_render('YourModule3Bundle:MenuBuilder:module3Menu', {'currentClass': 'active'}) }}
</ul>
在您的 Module1 捆绑目录中:
- 创建
Menu
文件夹
- 创建
MenuBuilder.php
如下:
-
<?php
namespace YourMenu1Bundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class MenuBuilder extends ContainerAware
{
public function applicationMenu(FactoryInterface $factory, array $options)
{
$rootMenu = $factory->createItem('rootMenu');
$rootMenu->setChildrenAttributes(array('class' => 'nav'));
$rootMenu->setCurrentUri($this->container->get('request')->getRequestUri());
$menu = $rootMenu->addChild('Module 1', array('route' => 'the_wanted_route'));
return $rootMenu;
}
}