Search by

athos99 / symfony-menu-bundle

athos99

A Symfony bundle to dynamically manage menus from a controller or a twig view, and to statically configure one or more menus

Package info

github.com/athos99/php-symfony-menu-bundle

Type:symfony-bundle

pkg:composer/athos99/symfony-menu-bundle

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.2 2026-12-03 21:09 UTC

This package is auto-updated.

Last update: 2026-12-03 21:14:53 UTC


README

A Symfony bundle to dynamically manage menus from a controller or a twig view, and to statically configure one or more menus. Each static menu corresponds to a class.

For Symfony 6.4, 7.x, 8.x

Usage

You can manage menus dynamically in PHP (from a controller) or in a twig view, and you can also declare static menus in your yaml configuration.

---
#config/packages/menu.yaml
menu:
  menus:
    main:
      class: App\Menu\Menu
    admin:
      class: App\Menu\MenuAdmin
...

Each menu class extends Athos99\MenuBundle\Menu and builds its items in the constructor through the menuTwig property:

namespace App\Menu;

use Athos99\MenuBundle\Menu;
use Athos99\MenuBundle\MenuItem;
use Athos99\MenuBundle\MenuTwig;

class Menu extends Menu
{
    public function __construct(MenuTwig $menuTwig)
    {
        parent::__construct($menuTwig);

        $menuTwig->addItem(
            'redaction_left',
            new MenuItem('homepage', 'Home', 'homepage')
        );
        $menuTwig->addItem(
            'redaction_left',
            new MenuItem('admin', 'Administration', null, [], [], [
                new MenuItem('admin_page', 'Information', 'admin_page'),
            ])
        );
    }
}

From a controller

use Athos99\MenuBundle\Menu;

class BaseController extends AbstractController
{
    public function __construct(private Menu $menu)
    {
    }

    public function index(): Response
    {
        $this->menu->menuTwig->addItem(
            'redaction_right',
            new MenuItem('idConnexion', 'Hello', 'info')
        );

        return $this->render('index.html.twig');
    }
}

From a twig view

The MenuTwig extension is automatically registered and provides:

getMenuItems(name)
getMenuSubItems(item)
getMenuOptions(name)
isMenuItemActive(item)
isUrlMenuItemActive(item)
addMenuItem(name, item)
newMenuItem(id, label, route, routeParams, options, subItems)
<ul class="navbar-nav">
  {% for item in getMenuItems('redaction_left') %}
    <li class="nav-item">
      <a href="{{ item.url }}" class="nav-link {{ isMenuItemActive(item) ? 'active' : '' }}">
        {{ item.label|raw }}
      </a>
    </li>
  {% endfor %}
</ul>

Installation

The bundle should be automatically enabled by Symfony Flex. If you don't use Flex, you'll need to enable it manually as explained in the docs.

composer config extra.symfony.allow-contrib true
composer require athos99/symfony-menu-bundle

Documentation

See documentation.md for full documentation with examples (static class menus, YAML-only menus, dynamic menus from a controller or a twig view, security, active items, etc.).

License Released under the Apache-2.0 license

Quality and test code

Code coverage : coverage line coverage branche

phpunit phpstan rector php-cs-fixer