外部网的每个包都是一个独立的应用程序。在我的菜单中,每个应用程序都被列出,我喜欢根据实际的路由前缀来标记当前的应用程序。
首先是base.html.twig中的小枝代码:
{{ knp_menu_render('AppBundle:Builder:mainMenu', { 'currentClass': 'active'}) }}建筑商功能:
public function mainMenu(FactoryInterface $factory, array $options){
$main = $factory->createItem('root');
foreach($this->getExtranetBundles() as $bundle){
$main->addChild($bundle->getAcronym(), array('route' => $bundle->getRoute()));
}
// Current Element
$matcher = new Matcher();
$matcher->addVoter(new BundleVoter($this->getCurrentBundle()));
$renderer = new ListRenderer($matcher);
$renderer->render($main);
return $main;
}如果找到当前菜单,我的BundleVoter类将正确工作,并返回true。但是在HTML中,当前元素从未包含“活动”类。
我在KnpMenuBundle中读到了更多的内容,并在Knp\Menu\Matcher类中添加了一些调试代码:
public function addVoter(VoterInterface $voter)
{
echo "add voter: " . get_class($voter);
$this->voters[] = $voter;
}得到了这个输出:
add voter: AppBundle\Menu\BundleVoter
add voter: Knp\Menu\Matcher\Voter\RouteVoter神秘的RouteVoter是从哪里来的?它是否覆盖了我对当前元素的BundleVoter选择?我怎样才能禁用/覆盖它呢?
发布于 2015-01-18 18:07:24
找到了更改标准knp_menu类的方法。我编辑了services.yml文件如下:
parameters:
knp_menu.voter.router.class: AppBundle\Menu\BundleVoter
services:
appbundle.menu.voter.request:
class: AppBundle\Menu\BundleVoter
arguments: [@service_container]
tags:
- { name: knp_menu.voter }类仍然被实例化了两次,不幸的是,我必须检查传递的参数是否为空。$container参数必须是可选的..。
class BundleVoter implements VoterInterface
{
private $container;
public function __construct($container = null)
{
if($container != null)
$this->container = $container;
}
public function matchItem(ItemInterface $item)
{
if($this->container != null){
$bundle = $this->container->get('menubundles')->getCurrentBundle();
if (null === $bundle || null === $item->getName()) {
return null;
}
if ($item->getName() == $bundle->getAcronym()) {
return true;
}
}
return null;
}
}如果您找到了更好的解决方案,请写:-) Thx
https://stackoverflow.com/questions/28009619
复制相似问题