dama / menu-bundle
Installs: 46 195
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 4
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.4 || ^8.0
- symfony/framework-bundle: ^5.4 || ^6.4 || ^7.1
- symfony/twig-bundle: ^5.4 || ^6.4 || ^7.1
- twig/twig: ^1.35 || ^2.4.4 || ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.27
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.5
- phpstan/phpstan-symfony: ^1.0
- phpunit/phpunit: ^8.5 || ^9.5
- symfony/browser-kit: ^5.4 || ^6.4 || ^7.1
- symfony/css-selector: ^5.4 || ^6.4 || ^7.1
- symfony/expression-language: ^5.4 || ^6.4 || ^7.1
- symfony/phpunit-bridge: ^7.1
- symfony/security-bundle: ^5.4 || ^6.4 || ^7.1
- symfony/yaml: 5.4 || ^6.4 || ^7.1
README
This bundle can be used to build dynamic menus based on granted permissions.
Step 1: create MenuTreeBuilder
class MainMenuTreeBuilder implements MenuTreeBuilderInterface { public function buildTree(Node $root) { $root ->child('social_media') ->setAttr('id', 'main_menu_social_media') ->setRequiredPermissions(['ROLE_SOCIAL_MENU']) ->child('stream') ->setRoute('social_media_stream') ->setRequiredPermissions(['ROLE_SOCIAL_STREAM']) ->setAdditionalActiveRequestMatcher(static function (Request $request): bool { // additionally will mark node as active if the request path starts with '/foo_bar' return str_starts_with($request->getPathInfo(), '/foo_bar'); }) ->end() ->child('update_status') ->setRoute('social_media_update_status') ->setRequiredPermissions(['ROLE_SOCIAL_UPDATE_STATUS']) ->end() ->ifTrue($someCondition) // only add child node(s) inside if the condition is true ->child('statistics') ->setRoute('social_media_statistics') ->setRequiredPermissions([new Expression("has_role('ROLE_USER')")]) ->end() ->endIf() ->end() ; } }
Step 2: add config for your menu
dama_menu:
menues:
my_main_menu:
tree_builder: Your\Namespace\MainMenuTreeBuilder #service ID OR FQCN and no constructor args
twig_template: YourNamespace:my_main_menu.html.twig #optional
Step 3: render the menu
{{ dama_menu_render('my_main_menu', {'collapse':true, 'nested':false}) }}