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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor