Created
December 1, 2012 18:18
-
-
Save dework/4183696 to your computer and use it in GitHub Desktop.
Symfony 2 breadcrumbs based on KnpMenuBundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% block breadcrumbs %} | |
<div id="breadcrumb"> | |
{{ knp_menu_render('breadcrumbs', {currentClass: 'active','template': 'ChyriusPublicBundle:Menu:breadcrumb.html.twig'}) }} | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
have you implemented the fix in your code above?
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
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