Symfony 2 breadcrumbs with KnpMenu

蓝咒 提交于 2019-12-11 07:24:39

问题


I use stable versions of KnpMenu and KnpMenuBundle - v.1.2.0 I can't use v.2, because of some dependencies.

And I'm trying to build breadcrumbs, depending on my menu.

Let's start with menu. I have my MenuBuilder class:

$menu = $this->factory->createItem('main');
$menu->setLabel('Main');
$menu->addChild('home', array('route' => 'home','label'=>'Home'));
$menu['home']->addChild('level1',array('route' => 'level1', 'label' => 'Level1'));
$menu['home']['level1']->addChild('level2',array('route' => 'level2', 'label' => 'Level2'));

The Knp\Menu\MenuItem class has a getBreadcrumbsArray() method. It should return an array of items in the current active menu trail. If you are on an earlier version of KnpMenu (<= 1.1.2, my version), the returned array will be in the form of label => uri.

I guess, my breadcrumbs, when I load level2 route should look similar to:

Home -> Level1 -> Level2

because Leve1 is the parent for Level2, and Home is also its ancestor.

Instead of that, getBreadcrumbsArray() method call in my Twig template produces following data:

array(2) {
  ["Main"]=>
  NULL
  ["Home"]=>
  string(25) "/symfony/web/app_dev.php/"
}

No Level1, no Level2, only Home. What is wrong? Is it supposed to work that way? What am I doing wrong?


回答1:


for Knp v1, should try to that to generate childrens:

$menu = $this->factory->createItem('root');
$menu->addChild['Main']
     ->seLabel['home']
$menu['Main']->addchild('level1', array('route', '@route1'))
$menu['Main']['level1']->addChild('level2', array('route', '@route2'))
{% knp_menu_get('Builder:BreadCrumb').getBreadcrumbsArray() %}


来源:https://stackoverflow.com/questions/17347694/symfony-2-breadcrumbs-with-knpmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!