Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Menu Bundle Laravel Package

dahovitech/menu-bundle

Bundle Symfony pour créer, configurer et afficher des menus via YAML : items hiérarchiques, templates Twig par menu, attributs HTML dynamiques, paramètres de route, gestion de l’état actif, ARIA, visibilité via voters, events et cache intégré.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require dahovitech/menu-bundle:dev-main
    

    Enable in config/bundles.php:

    Dahovitech\MenuBundle\DahovitechMenuBundle::class => ["all" => true],
    
  2. Basic Configuration: Create config/packages/dahovitech_menu.yaml:

    dahovitech_menu:
        menus:
            main_menu:
                items:
                    - label: 'Home'
                      route: 'app_home'
    
  3. First Use Case: Render in Twig:

    {{ dahovitech_menu('main_menu') }}
    

Implementation Patterns

1. Menu Definition & Hierarchy

  • Nested Menus: Define parent-child relationships via children:
    items:
        - label: 'Services'
          children:
              - label: 'Web Design'
                route: 'app_web_design'
    
  • Dynamic Attributes: Customize HTML attributes per item:
    items:
        - label: 'Contact'
          route: 'app_contact'
          attributes:
              class: 'btn btn-primary'
              data-toggle: 'modal'
    

2. Route Parameters & URL Generation

Pass parameters to routes:

items:
    - label: 'User Profile'
      route: 'app_user_profile'
      route_params:
          id: 42

3. Template Customization

Override default templates per menu:

menus:
    admin_menu:
        template: '@App/menu/admin.html.twig'

4. Event-Driven Extensions

Modify menus dynamically via MenuBuildEvent:

// src/EventSubscriber/CustomMenuSubscriber.php
public function onMenuBuild(MenuBuildEvent $event) {
    if ($event->getMenuName() === 'main_menu') {
        $event->addItem(new MenuItem('Dynamic Item', '/dynamic'));
    }
}

5. Access Control (Voters)

Restrict menu items by role:

items:
    - label: 'Admin Panel'
      route: 'app_admin'
      voter: 'admin_voter'  // Custom voter service ID

Gotchas and Tips

Pitfalls

  1. Caching Quirks:

    • Clear cache after modifying menu definitions:
      php bin/console cache:clear
      
    • Use cache:pool-clear dahovitech_menu.cache for targeted clearing.
  2. Route Parameter Conflicts:

    • Ensure route_params keys match the route definition exactly (case-sensitive).
  3. Voter Priority:

    • Voters are evaluated in service container order. Use priority tags to control evaluation sequence.

Debugging Tips

  • Dump Menu Structure:
    {{ dump(dahovitech_menu('main_menu').getItems()) }}
    
  • Check Event Dispatching: Enable Symfony’s event dispatcher debug:
    framework:
        profiler:
            enabled: true
    

Extension Points

  1. Custom Menu Builders: Implement Dahovitech\MenuBundle\Builder\MenuBuilderInterface for alternative menu logic.

  2. Template Inheritance: Extend base templates (@DahovitechMenu/menu.html.twig) in your theme.

  3. ARIA Attributes: Add custom ARIA roles via attributes:

    attributes:
        aria-expanded: 'true'
    

Performance

  • Cache Configuration: Adjust TTL in config/packages/dahovitech_menu.yaml:
    dahovitech_menu:
        cache:
            enabled: true
            ttl: 3600  # 1 hour
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle