Symfony Bundle featuring:
- A factory for creating the navigation tree, using BuildDirectors which you can add to, if needed
- Twig functions for rendering navigation elements (tree, ancestry, breadcrumbs) and inspecting the navigation tree
composer require webfactory/navigation-bundle ... and activate the bundle in your kernel, depending on your Symfony version.
{{navigation_tree(root, maxLevels = 1, expandedLevel = 1, template = '@WebfactoryNavigation/Navigation/navigation.html.twig') }} {{navigation_tree(root ={"webfactory_pages.page_id": root_page_id}) }}{{navigation_tree( root ={"webfactory_pages.page_id": root_page_id, "_locale": app.request.locale}, template = 'AppBundle:Navigation:navigation.html.twig' ) }} An ancestry list is the active path from the given start level to the currently active node. Useful if you want to render e.g. the third level navigation outside of the regular navigation.
{{navigation_ancestry(startLevel, maxLevels = 1, expandedLevels = 1, template = '@WebfactoryNavigation/Navigation/navigation.html.twig') }} {{navigation_ancestry(startLevel = 1) }}{{navigation_ancestry(startLevel = 1, template = '@App/Navigation/secondaryNav.html.twig') }} {{navigation_breadcrumbs(template = '@WebfactoryNavigation/Navigation/breadcrumbs.html.twig') }} {{navigation_breadcrumbs() }}{{navigation_breadcrumbs(template = '@App/Navigation/breadcrumbs.html.twig') }} For each function mentioned above you can provide a Twig template in which you can extend the base template and overwrite each block. Please find the default blocks in src/Resources/views/Navigation/navigationBlocks.html.twig.
Example:
{# layout.html.twig: #} ...{{navigation_tree(root={"webfactory_pages.page_id": root_page_id}, template='AppBundle:Navigation:navigation.html.twig') }} ...{# AppBundle:Navigation:navigation.html.twig: #}{% extends"@WebfactoryNavigation/Navigation/navigation.html.twig" %}{% blocknavigation_list %} <navclass="project-specific-classes">{{parent() }} </nav>{% endblock %}Implement a Webfactory\Bundle\NavigationBundle\Build\BuildDirector. Example:
<?phpnamespaceAppBundle\Navigation; useJMS\ObjectRouting\ObjectRouter; useSymfony\Component\Config\Resource\FileResource; useWebfactory\Bundle\NavigationBundle\Build\BuildContext; useWebfactory\Bundle\NavigationBundle\Build\BuildDirector; useWebfactory\Bundle\NavigationBundle\Build\BuildDispatcher; useWebfactory\Bundle\NavigationBundle\Tree\Tree; useWebfactory\Bundle\WfdMetaBundle\Config\DoctrineEntityClassResource; finalclass KeyActionBuildDirector implements BuildDirector{/** @var YourEntityRepository */private$repository; /** @var ObjectRouter */private$objectRouter; publicfunction__construct(YourEntityRepository$repository, ObjectRouter$objectRouter){$this->repository = $repository; $this->objectRouter = $objectRouter} publicfunctionbuild(BuildContext$context, Tree$tree, BuildDispatcher$dispatcher): void{if (!$this->isInterestedInContext($context)){return} foreach ($this->repository->findForMenu() as$entity){$context->get('node') ->addChild() ->set('caption', $entity->getName()) ->set('visible', true) ->set('url', $this->objectRouter->path('detail', $entity))} $this->addResourcesToTreeCache($dispatcher)} privatefunctionaddResourcesToTreeCache(BuildDispatcher$dispatcher): void{$dispatcher->addResource(newFileResource(__FILE__)); $dispatcher->addResource(newDoctrineEntityClassResource(YourEntity::class))} }Define your implementation as a service and tag it webfactory_navigation.build_director. Example:
<serviceclass="AppBundle\Navigation\YouEntityBuildDirector"> <argumenttype="service"id="AppBundle\Repository\YourEntityRepository" /> <argumenttype="service"id="JMS\ObjectRouting\ObjectRouter" /> <tagname="webfactory_navigation.build_director"/> </service>See src/Resources/doc/How-To-Use-Klassendiagramm.puml for more.
This project was started at webfactory GmbH, Bonn.
Copyright 2015 - 2025 webfactory GmbH, Bonn. Code released under the MIT license.