athos99 / symfony-menu-bundle
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
Requires
- php: >=8.1
- symfony/config: >=6.4
- symfony/dependency-injection: >=6.4
- symfony/http-foundation: >=6.4
- symfony/http-kernel: >=6.4
- symfony/routing: >=6.4
- twig/twig: >=3.4
Requires (Dev)
- ekino/phpstan-banned-code: ^3.0
- friendsofphp/php-cs-fixer: ^3.49
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.4
- rector/rector: ^2.0.11
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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