使用KnpMenu的Symfony 2面包屑 [英] Symfony 2 breadcrumbs with KnpMenu

查看:99
本文介绍了使用KnpMenu的Symfony 2面包屑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 KnpMenu KnpMenuBundle 的稳定版本- v.1.2.0
我无法使用v.2

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

让我们从菜单开始。我有我的MenuBuilder类:

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'));

Knp\Menu\MenuItem 类具有 getBreadcrumbsArray()方法。它应返回当前活动菜单路径中的项目数组。如果您使用的是KnpMenu的早期版本(< = 1.1.2,我的版本),则返回的数组将以label => uri的形式出现。

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.

我想,我的面包屑在加载 level2 路线时应该类似于:

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

首页- > Level1-> Level2

因为Leve1是Level2的父级,而Home也是其祖先。

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

相反,我的Twig模板中的 getBreadcrumbsArray()方法调用会产生以下数据:

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/"
}

没有 Level1 ,没有 Level2 ,只有主页。怎么了?它应该以这种方式工作吗?我在做什么错?

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

推荐答案

对于Knp v1,应该尝试生成孩子:

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() %}

这篇关于使用KnpMenu的Symfony 2面包屑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆