Skip to content

Instantly share code, notes, and snippets.

@dework
Created December 1, 2012 18:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dework/4183696 to your computer and use it in GitHub Desktop.
Save dework/4183696 to your computer and use it in GitHub Desktop.
Symfony 2 breadcrumbs based on KnpMenuBundle
{% block breadcrumbs %}
<div id="breadcrumb">
{{ knp_menu_render('breadcrumbs', {currentClass: 'active','template': 'ChyriusPublicBundle:Menu:breadcrumb.html.twig'}) }}
</div>
{% endblock %}
{% block root %}
{% if item.breadcrumbsArray %}
{%- for link in item.breadcrumbsArray %}
{%- if loop.index == 1 %}<a href="/" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
{%- elseif not loop.last %}<a href="{{ link.uri }}">{{ link.label | trans }}</a>
{%- else %}
<a href="{{ link.uri }}" class="current">{{ link.label | trans }}</a>
{%- endif %}
{%- endfor %}
{% endif %}
{% endblock %}
<?php
namespace Chyrius\PublicBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Chyrius\PublicBundle\Menu\RequestVoter;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuBuilder
{
private $factory;
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory, ContainerInterface $container)
{
$this->factory = $factory;
$this->container = $container;
}
public function createMainMenu(Request $request)
{
$menu = $this->factory->createItem('root');
$menu->setLabel('Home');
$menu->addChild('Dashboard', array('route' => 'dashboard'));
$menu->addChild('Desk', array('route' => 'desk'));
$menu->addChild('Statistic', array('route' => 'stat'));
//...
return $menu;
}
public function createBreadcrumbsMenu(Request $request) {
//
$bcmenu = $this->createMainMenu($request);
return $this->getCurrentMenuItem($bcmenu);
}
public function getCurrentMenuItem($menu)
{
$voter = $this->container->get('chyrius.public.menu.voter.request');
foreach ($menu as $item) {
if ($voter->matchItem($item)) {
return $item;
}
if ($item->getChildren() && $currentChild = $this->getCurrentMenuItem($item)) {
return $currentChild;
}
}
return null;
}
}
<?php
namespace Chyrius\PublicBundle\Menu;
use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Voter\VoterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RequestVoter implements VoterInterface
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function matchItem(ItemInterface $item)
{
if ($item->getUri() === $this->container->get('request')->getRequestUri()) {
return true;
} else {
if ($item->getUri() !== '/' && (substr(
$this->container->get('request')->getRequestUri(),
0,
strlen($item->getUri())
) === $item->getUri())
) {
return true;
}
}
return null;
}
}
services:
chyrius.public.menu.builder:
class: Chyrius\Public\Menu\MenuBuilder
arguments: ["@knp_menu.factory", "@service_container"]
chyrius.public.menu.main:
class: Knp\Menu\MenuItem
factory_service: chyrius.public.menu.builder
factory_method: createMainMenu
arguments: ["@request"]
scope: request
tags:
- { name: knp_menu.menu, alias: main }
chyrius.public.menu.breadcrumbs:
class: Knp\Menu\MenuItem
factory_service: chyrius.public.menu.builder
factory_method: createBreadcrumbsMenu
arguments: ["@request"]
scope: request
tags:
- { name: knp_menu.menu, alias: breadcrumbs }
chyrius.public.menu.voter.request:
class: Chyrius\Public\Menu\RequestVoter
arguments:
- @service_container
tags:
- { name: knp_menu.voter }
@xtrasmal
Copy link

breadcrumbArray does not exist?

@iBasit
Copy link

iBasit commented May 31, 2015

Since version 2.0, getBreadcrumbsArray has been moved to Knp\Menu\Util\MenuManipulator.

Following is the solution added to the above code.

http://stackoverflow.com/a/28851780/75799

@jstgermain
Copy link

have you implemented the fix in your code above?

@Van-peterson
Copy link

I'm guessing the following error after using the code above.
Call to a member function get() on null
Error line : $voter = $this->container->get('chyrius.public.menu.voter.request');
Some help ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment