KnpLabs/KnpMenuBundle — Issue #100: Please, add menu builder context to knp menu render
Author: zerkalica
Created: 2012-02-12 13:46:45+00:00
State: open

I develop MillwrightMenuBundle for grid-based web applications.

Main idea: each internal link on the site is a part of configured menu container, which supports translation, role and acl-based security, route parameters. All menu items has static options: labels, translation, etc and context: currentUri, security, route parameters.

Many model-based controllers has 4 actions: index, create, edit, delete. All models secured by acl and have permissions: view, edit, delete.

When building menu in template, need to hide some links in menu if they denied from security and setup uri to each item. For actions view, edit, delete i need to setup route parameters in template and pass them to menu builder.

``` php
# src/Application/Millwright/CoreBundle/Controller/ArticleController.php
...
/**
* @Menu(translateDomain="MillwrightMenuBundle")
*/
class ArticleController extends Controller
{
/**
* @Route("/articles", name="article_index")
* @Template()
* @Secure(roles="ROLE_USER")
*/
public function indexAction() {
//
}

/**
* @Route("/article/{article}", name="article_view")
* @Secure(roles="ROLE_USER")
* @SecureParam(name="article", permissions="VIEW")
* @Template()
* @Menu(label="View")
*/
public function viewAction(Article $article) {
return array('article' => $article);
}
}
```

Index action template:
```
{% for article in articles %}
{{ millwright_menu_render('article_actions', {article: article.id}) }}
{% endfor %}
```

Menu config file:

```yaml
# app/config/menu.yml

millwright_menu:
tree:
article_index_actions:
children:
article_create: ~

article_actions:
children:
article_view: ~
article_edit: ~
article_delete: ~
```

Knp Bundle MenuProviderInterface in twig helper has get method, which returns root MenuItem instance. I think, the better way is return MenuBuilder instance with method createMenu($name, $context) and setup context in render helper.

I use own twig helpers in my bundle, but a lot of copypast from knp menu.

Why do not use menu builder itself as knp_menu.menu tagged service and pass route parameters from renderer ?

```yaml
# src/Acme/MainBundle/Resources/config/services.yml
services:
acme_main.menu_builder:
tags:
- { name: knp_menu.menu, alias: main } # The alias is what is used to retrieve the menu

class: Acme\MainBundle\Menu\MenuBuilder
arguments: ["@knp_menu.factory"]
```