The Knp\Menu\MenuItem class has a getBreadcrumbsArray() method. It should return an array of items in the current active menu. If you are using an earlier version of KnpMenu (<= 1.1.2, I think), the returned array will look like label => uri . Otherwise, it will be an array with each element having the keys label , uri and item .
To find the current menu item, you probably want to create a method in your controller (or somewhere else, if that makes sense for your project), looks something like this:
public function getCurrentMenuItem($menu) { foreach ($menu as $item) { if ($item->isCurrent()) { return $item; } if ($item->getChildren() && $current_child = $this->getCurrentMenuItem($item)) { return $current_child; } } return null; }
From there, you can call getBreadcrumbsArray() on the return value:
$this->getCurrentMenuItem($your_menu)->getBreadcrumbsArray();
I assume that I would eventually create a Twig extension that registers the global breadcrumbs , and put the getCurrentMenuItem() method in there. This way you can have the breadcrumb variable in all of your templates without having to manually display it in each controller.
Source: https://github.com/KnpLabs/KnpMenu/blob/master/src/Knp/Menu/MenuItem.php#L544 .
source share