Installation:
composer require awaresoft/menu-bundle
Note: Due to symlinking requirements, follow the README’s local setup instructions if modifying the bundle.
Enable the Bundle:
Add to config/bundles.php:
Awaresoft\MenuBundle\AwaresoftMenuBundle::class => ['all' => true],
First Use Case: Generate a menu in a Twig template:
{{ knp_menu_render('main_menu', {
'current_uri': app.request.uri,
'deep': true,
'template': '@AwaresoftMenu/menu.html.twig'
}) }}
Default template: @AwaresoftMenu/menu.html.twig (override via config).
Configuration:
Override defaults in config/packages/awaresoft_menu.yaml:
awaresoft_menu:
templates:
menu: 'path/to/custom_template.html.twig'
sonata_integration: true # Enable if using SonataAdmin
Dynamic Menu Building:
$menu = $this->menuFactory->createItem('root');
$menu->addChild('Dashboard', ['route' => 'dashboard']);
$this->menuFactory->add('main_menu', $menu);
# config/packages/awaresoft_menu.yaml
awaresoft_menu:
cache: true
SonataAdmin Integration:
use Awaresoft\MenuBundle\Annotation\Menu;
use Sonata\AdminBundle\Annotation\Route;
/**
* @Menu("admin_menu")
* @Route("admin", name="admin_dashboard")
*/
class DashboardController extends Controller { ... }
php bin/console awaresoft:menu:build
Twig Extensions:
{% set menu = app.service('awaresoft.menu.menu_factory').get('main_menu') %}
{{ menu|knp_menu_render({deep: true}) }}
Multi-Language Menus:
$menu = $this->menuFactory->createItem('root', ['locale' => 'en']);
$menu->addChild('Home', ['route' => 'home_en']);
Event Listeners:
Subscribe to awaresoft.menu.build to modify menus dynamically:
use Awaresoft\MenuBundle\Event\MenuBuildEvent;
public function onMenuBuild(MenuBuildEvent $event) {
$event->getMenu()->addChild('Custom Item', ['route' => 'custom']);
}
Register in services.yaml:
services:
App\EventListener\MenuListener:
tags:
- { name: kernel.event_listener, event: awaresoft.menu.build, method: onMenuBuild }
Doctrine Fixtures: Pre-populate menus in fixtures:
public function load(ObjectManager $manager) {
$menuFactory = $this->container->get('awaresoft.menu.menu_factory');
$menu = $menuFactory->createItem('fixture_menu');
$menu->addChild('Test', ['route' => 'test']);
$menuFactory->add('fixture_menu', $menu);
}
Symlinking Issues:
composer remove awaresoft/menu-bundle and clear cache (php bin/console cache:clear).Caching Quirks:
php bin/console awaresoft:menu:clear-cache
awaresoft_menu.yaml during development:
awaresoft_menu:
cache: false
SonataAdmin Conflicts:
sonata_integration: true and rebuild menus:
php bin/console awaresoft:menu:build --env=prod
Route Resolution:
{{ path('dashboard') }} {# Instead of hardcoded URLs #}
php bin/console awaresoft:menu:debug main_menu
twig:
strict_variables: true
Custom Templates:
Override the default template by copying @AwaresoftMenu/menu.html.twig to your theme and updating the config:
awaresoft_menu:
templates:
menu: 'YourThemeBundle:menu:custom.html.twig'
Menu Providers: Create a custom provider to fetch menus from external sources (e.g., API):
use Awaresoft\MenuBundle\Provider\MenuProviderInterface;
class ApiMenuProvider implements MenuProviderInterface {
public function getMenu(string $name) { ... }
}
Register in services.yaml:
services:
App\Provider\ApiMenuProvider:
tags:
- { name: awaresoft.menu.provider, alias: 'api_menu' }
Menu Builders: Extend the base builder for custom logic:
use Awaresoft\MenuBundle\Builder\MenuBuilder;
class CustomMenuBuilder extends MenuBuilder {
protected function buildMenu() {
// Custom logic
$this->addChild('Custom Root', ['route' => 'custom_root']);
}
}
Register as a service:
services:
App\Builder\CustomMenuBuilder:
tags:
- { name: awaresoft.menu.builder, alias: 'custom_menu' }
awaresoft_menu:
locale_aware: true
$menu->addChild('High Priority', ['route' => 'high'], ['priority' => 10]);
How can I help you explore Laravel packages today?