symfony-cmf/menu-bundle
Symfony CMF MenuBundle integrates dynamic, CMS-driven navigation into Symfony apps, building menus from content repositories with rich node metadata. Provides menu rendering, routing-aware items, and admin-friendly structure for complex site navigation.
Installation
Add the bundle to your composer.json:
composer require symfony-cmf/menu-bundle
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
SymfonyCmf\Bundle\MenuBundle\SymfonyCmfMenuBundle::class => ['all' => true],
];
Configuration
Extend KnpMenuBundle config in config/packages/knp_menu.yaml:
knp_menu:
twig:
template: 'SymfonyCmfMenuBundle:Menu:menu.html.twig'
repositories:
- SymfonyCmf\Bundle\MenuBundle\Repository\PhpcrMenuRepository
First Use Case
Create a PHPCR document for a menu (e.g., MenuNode):
use SymfonyCmf\Bundle\MenuBundle\Document\MenuNode;
$menuNode = new MenuNode();
$menuNode->setTitle('Main Menu');
$dm->persist($menuNode);
$dm->flush();
Render it in Twig:
{{ knp_menu_render('main_menu', {'rootNode': menuNode}) }}
Dynamic Menu Generation Use PHPCR queries to fetch menu nodes dynamically:
$menuRepository = $dm->getRepository(MenuNode::class);
$menuItems = $menuRepository->findBy(['parent' => $parentNode]);
Caching Strategies
Leverage Symfony’s cache system with knp_menu.cache:
knp_menu:
cache:
prefix: 'cmf_menu_'
default_lifetime: 3600
Integration with SymfonyCmf Routing
Link menu nodes to routes via MenuNode properties:
$menuNode->setRoute('homepage');
$menuNode->setRouteParameters(['_locale' => 'en']);
Twig Extensions Customize menu rendering with Twig:
{% for item in menu %}
<li class="{{ item.isActive ? 'active' }}">
<a href="{{ path(item.route, item.routeParameters) }}">{{ item.title }}</a>
</li>
{% endfor %}
Event Listeners
Hook into MenuBuilder events for dynamic modifications:
use SymfonyCmf\Bundle\MenuBundle\Event\MenuBuilderEvent;
$event->getMenu()->addChild('admin', ['route' => 'admin_dashboard']);
PHPCR Dependency
doctrine/doctrine-phpcr-odm-bundle). Ensure it’s installed and configured.php bin/console doctrine:phpcr:debug
Menu Caching Quirks
php bin/console cache:clear
knp_menu.cache_lifetime: 0 for development to disable caching.Route Resolution
MenuNode routes exist in Symfony’s router. Use:
$menuNode->setRoute('non_existent_route'); // Throws exception if route doesn’t exist.
Archived Package Risks
cmf/routing for new projects.Debugging Menu Structure Dump menu nodes in Twig:
{{ dump(menu) }}
Or in PHP:
var_dump($menuNode->getChildren());
Performance
findBy with HINT_LOOKUP for large menus:
$qb = $dm->createQueryBuilder(MenuNode::class);
$qb->where('node.parent = :parent')->setHint('HINT_LOOKUP', true);
Custom Document Types
Extend MenuNode for domain-specific fields:
use SymfonyCmf\Bundle\MenuBundle\Document\MenuNode as BaseMenuNode;
class CustomMenuNode extends BaseMenuNode {
private $customField;
// ...
}
Symfony 5+ Compatibility
SymfonyCmfMenuBundle in config/bundles.php to force autoloading:
SymfonyCmf\Bundle\MenuBundle\SymfonyCmfMenuBundle::class => ['all' => true, 'autoload' => true],
Testing Mock PHPCR in tests:
$this->dm->expects($this)->method('find')->willReturn($menuNode);
How can I help you explore Laravel packages today?