KnpMenuBundle the easy way breadcrumb !

In a previous post, I explain how to make a breadcrumb who work with the KnpMenuBundle.
Unfortunately this post was obsolete (and for a while). The staff of the KnpMenuBundle have deprecated the useful function getBreadcrumbsArray

I don’t find any solution, so I’ve made my own. The CnertaBreadcrumpBundle.
Now it’s a piece of cake to create a breadcrumb with KnpMenuBundle.

You can find the documentation on github.
Here, I just want to show you how to use it a bit more deeply than in the documentation.

Below the Builder class. This is just a classical builder from the KnpMenuBudle.

namespace WaldoColorfullBundleMenu;
use KnpMenuFactoryInterface;
use SymfonyComponentDependencyInjectionContainerAware;
class Builder extends ContainerAware
  public function mainMenu(FactoryInterface $factory, array $options)
      $menu = $factory->createItem('main');
      $menu->addChild('Entrée 1', array('route' => '_route_entre_1'))
              ->addChild('Entrée 1.1', array('route' => '_route_entre_1_1'))
              ->addChild('Entrée 1.1.1', array('route' => '_route_entre_1_1_1'));
      $menu->addChild('Entrée 2', array('route' => '_route_entre_1'))
              ->addChild('Entrée 2.1', array('route' => '_route_entre_2_1'))
              ->addChild('Entrée 2.1.1', array('route' => '_route_entre_2_1_1'));
      return $menu;

In this class we just put a classical menu definition.
Now if we want to render a breadcrumb in our Twig template, let me show you how too easy it is.

{{ cnerta_breadcrumb_render('WaldoColorfullBundle:Builder:mainMenu') }}

Thats all. You just have to call the Helper cnerta_breadcrumb_render and give the path of your menu. Like the KnpMenuBundle.
Too easy ?
Now, if you want to customize your beardcrump in your template :

{% set currentItem = cnerta_breadcrumb_get('WaldoColorfullBundle:Builder:mainMenu') %}
{% for item in currentItem %}
   {% if loop.index != 1 %}
       {% if loop.index > 2 %} > {% endif %}
       {% if not loop.last %}<a href="{{ item.uri }}">{{ item.label }}</a>
       {% else %}<span>{{ item.label }}</span>{% endif %}
   {% endif %}
{% endfor %}

You just need to call The Helper cnerta_breadcrumb_get and give the path of your menu.
But if you want a clean code, you have to put your breadcrumb template in a separate file. To specifies the template, it’s like the KnpMenuBundle, you have to give an option array with the template path.

{{ cnerta_breadcrumb_render('WaldoColorfullBundle:Builder:mainMenu', {'template': 'MyWonderfullBundle:Breadcrumb:myBreadcrumb.html.twig'}) }}

The code and the architecture come from a big part from the KnpMenu and the KnpMenuBundle. This bundle is not perfect but it make the job.

2 Comments on “KnpMenuBundle the easy way breadcrumb !

  1. Bonjour

    que se passe t’il si on veut rajouter des items à droite du menu (nav navbar-nav navbar-right)?
    Je crée donc une autre fonction rightMenu
    et puis ?

    Merci beaucoup de votre réponse

Leave a Reply to cyphos Cancel reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.