What's the best way to create breadcrumbs using knpmenu bundle in symfony 2.1.x ? Aside from using 3-rd party bundles.
UPDATE:
Hi, theunraveler, sorry for late answer. Now I've been following your example and I'm stuck at one moment. Here, code below throws an exception, that
Missing argument 2 for Acme\DemoBundle\Menu\MenuBuilder::getBreadCrumbs()
{% set item = knp_menu_get('main') %}
{{ knp_menu_render(item) }}
{% block breadcrumbs %}
{% set breadcrumbs = knp_menu_get('breadcrumbs', [], {'request': app.request, 'menu': item }) %}
{{ dump(breadcrumbs) }}
{% endblock %}
Why it doesn't accepts "item" variable?
Since KnpMenu
2.1
, there is a new twig function:knp_menu_get_breadcrumbs_array
You can take a look at my gist: https://gist.github.com/fsevestre/b378606c4fd23814278a
I added a new twig function
knp_menu_get_current_item
, which retrieve the current menu item and work fine with theknp_menu_get_breadcrumbs_array
function.--
Edit:
With KnpMenu
2.2
, you can now do:https://github.com/KnpLabs/KnpMenu/blob/master/doc/02-Twig-Integration.markdown#functions
The
knp_menu_get_current_item('main')
Twig function will retrieve the current menu item, for themain
menu.Since version
2.0
,getBreadcrumbsArray
has been moved toKnp\Menu\Util\MenuManipulator
.Possible workout to this solution is to create a twig extension:
Register twig extension:
In breadcrumb.html.twig:
The
Knp\Menu\MenuItem
class has agetBreadcrumbsArray()
method. It should return an array of items in the current active menu trail. If you are on an earlier version ofKnpMenu
(<= 1.1.2, I think), the returned array will be in the form oflabel => uri
. Otherwise, it will be an array with each item having keyslabel
,uri
, anditem
.To find the current menu item, you'll probably want to create a method in your controller (or somewhere else, if it makes more sense for your project) that looks something like this:
From there, you can call
getBreadcrumbsArray()
on the returned value:I guess what I would do ultimately is create a Twig extension that registers a
breadcrumbs
global, and put thegetCurrentMenuItem()
method in there. That way, you can have thebreadcrumb
variable in all of your templates without having to manually render it in each controller.Source: https://github.com/KnpLabs/KnpMenu/blob/master/src/Knp/Menu/MenuItem.php#L544.