4

所以我建立了一个菜单,如下例所示:

<?php namespace AppBundle\Menu;

use Doctrine\ORM\EntityManager;
use Knp\Menu\FactoryInterface;
use Knp\Menu\MenuFactory;

class AdminMenuBuilder
{

    public function sidebarMenu(FactoryInterface $factory, array $options)
    {
    $menu = $factory->createItem('root', array(
        'navbar' => true,
        'childrenAttributes' => [
            'class' => 'nav main-menu',
        ],
    ));


    $menu->addChild('Dashboard')
         ->setAttributes([
            'icon' =>'fa fa-dashboard',
            'class' => 'dropdown',
             'dropdown' => true
         ]);

    $menu['Dashboard']->addChild('Details', ['route' => 'app.admin.dashboard']);
    $menu['Dashboard']->addChild('Details 2', ['route' => 'app.admin.dashboard']);

    $menu->addChild('Users', ['route' => 'app.admin.dashboard.users'])
        ->setAttribute('icon', 'fa fa-users');


        return $menu;
    }
}

如何使用 KNPMenuBundle v2 创建面包屑?我正在使用 symfony2 2.7.5

4

2 回答 2

3

KnpMenuBundle 2.1.0(几周前发布)有一个knp_menu_get_breadcrumbs_array()你可以使用的功能。例如:

{% set item = knp_menu_get('some_menu_item') %}
{% set breadcrumbs = knp_menu_get_breadcrumbs_array(item) %}
{% for breadcrumb in breadcrumbs %}
  <a href="{{ breadcrumb.url }}">{{ breadcrumb.label }}</a>
{% endfor %}
于 2015-10-10T08:05:47.463 回答
0

如果您有嵌套菜单,上述解决方案将不起作用。这是递归返回活动菜单项数组的扩展:

<?php

namespace AnalyticsBundle\Twig;

use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\MatcherInterface;
use Knp\Menu\Twig\Helper;

/**
 * Class MenuExtension
 * @package AnalyticsBundle\Twig
 */
class MenuExtension extends \Twig_Extension
{
    private $helper;

    private $matcher;

    public function __construct(Helper $helper, MatcherInterface $matcher)
    {
        $this->helper = $helper;
        $this->matcher = $matcher;
    }

    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('analytics_get_current_menu_items', function($menu, array $path = array(), array $options = array()) {
                $menu = $this->helper->get($menu, $path, $options);
                return $this->getCurrentItems($menu);
            }),
        ];
    }

    private function getCurrentItems(ItemInterface $item)
    {
        $items = [];
        $getActiveItems = function (ItemInterface $item) use (&$getActiveItems, &$items) {
            if ($this->matcher->isCurrent($item)) {
                $items[] = $item;
                foreach ($item->getChildren() as $child) {
                    $getActiveItems($child);
                }
            }
        };
        $getActiveItems($item);
        return $items;
    }

    public function getName()
    {
        return 'analytics_menu';
    }
}

示例用法:

                <ul class="page-breadcrumb">
                    {% for item in analytics_get_current_menu_items('main') %}
                        <li>
                            <span>{{ item.label }}</span>
                        {% if not loop.last %}
                            |
                        {% endif %}
                        </li>
                    {% endfor %}
                </ul>
于 2016-06-21T15:08:57.667 回答