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

desarrolla2/menu-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require desarrolla2/menu-bundle
    

    Verify composer.json includes the package under require.

  2. Enable the Bundle: Add to AppKernel.php:

    new Desarrolla2\MenuBundle\Desarrolla2MenuBundle(),
    

    (Note: The README incorrectly references Knp\Bundle\Desarrolla2\MenuBundle—use the correct namespace.)

  3. First Menu: Create a builder class (e.g., src/AdminBundle/Menu/MainMenu.php):

    namespace AdminBundle\Menu;
    use Desarrolla2\MenuBundle\Menu\MenuInterface;
    
    class MainMenu implements MenuInterface {
        public function getMenu() {
            return [
                'class' => 'main-menu',
                'items' => [
                    ['name' => 'Home', 'route' => 'homepage'],
                ],
            ];
        }
    }
    
  4. Register the Menu: Add to services.yml (or config/services.yaml in Symfony 4+):

    servicios.menu.main:
        class: AdminBundle\Menu\MainMenu
        tags:
            - { name: menu.builder, alias: main }
    
  5. Render the Menu: In a Twig template:

    {{ knp_menu_render('main') }}
    

Implementation Patterns

Core Workflows

  1. Menu Hierarchy: Use nested arrays for submenus:

    'items' => [
        [
            'name' => 'Parent',
            'items' => [
                ['name' => 'Child', 'route' => 'child_route'],
            ],
        ],
    ]
    
  2. Dynamic Menus: Inject services (e.g., Security, Router) into the builder:

    use Symfony\Component\Security\Core\SecurityContextInterface;
    
    class MainMenu {
        private $security;
    
        public function __construct(SecurityContextInterface $security) {
            $this->security = $security;
        }
    
        public function getMenu() {
            if (!$this->security->isGranted('ROLE_ADMIN')) {
                return [];
            }
            // ...
        }
    }
    
  3. Route-Based Menus: Use route keys for URL generation:

    'items' => [
        ['name' => 'Dashboard', 'route' => 'dashboard', 'routeParameters' => ['id' => 1]],
    ]
    
  4. Twig Integration:

    • Render: {{ knp_menu_render('alias') }}
    • Customize: Pass options:
      {{ knp_menu_render('main', { 'depth': 2, 'currentClass': 'active' }) }}
      
  5. Event-Driven Extensions: Listen to menu.build events to modify menus globally:

    services.menu.listener:
        class: AppBundle\EventListener\MenuListener
        tags:
            - { name: kernel.event_listener, event: menu.build, method: onBuildMenu }
    

Gotchas and Tips

Pitfalls

  1. Namespace Mismatch: The README references Knp\Bundle\Desarrolla2\MenuBundle, but the correct namespace is Desarrolla2\MenuBundle\Desarrolla2MenuBundle. Double-check in AppKernel.php.

  2. Service Tagging: Ensure the menu.builder tag uses the correct alias (e.g., alias: main). Misspelled aliases will break rendering.

  3. Route Resolution: If routes fail, verify:

    • The route exists in routing.yml/config/routes.yaml.
    • routeParameters are correctly formatted (e.g., ['id' => 1]).
  4. Caching: Menus are cached by default. Clear the cache after changes:

    php bin/console cache:clear
    
  5. Deprecated Symfony 2: This bundle is designed for Symfony 2.x. If using Symfony 4/5/6, expect compatibility issues (e.g., AppKernel, services.yml syntax).

Debugging Tips

  1. Dump Menu Structure: Add a temporary dump in the builder:

    var_dump($this->getMenu()); die();
    
  2. Check Registered Menus: Run:

    php bin/console debug:container | grep menu.builder
    
  3. Twig Debugging: Enable Twig debug mode in config/packages/twig.yaml:

    twig:
        debug: true
        strict_variables: true
    

Extension Points

  1. Custom Menu Items: Extend the MenuInterface to add metadata (e.g., priority, permissions):

    interface MenuInterface {
        public function getMenu();
        public function getPriority(); // e.g., return 100;
    }
    
  2. Override Twig Rendering: Create a custom Twig extension to modify the default template:

    class CustomMenuExtension extends \Twig_Extension {
        public function getFunctions() {
            return [
                new \Twig_SimpleFunction('custom_menu_render', [$this, 'renderMenu']),
            ];
        }
    }
    
  3. Database-Driven Menus: Fetch menu items from a DB in getMenu() and structure them into the expected array format.

  4. AJAX Menus: Use the ajax option in menu items to enable dynamic loading:

    'items' => [
        [
            'name' => 'Dynamic Content',
            'route' => 'ajax_route',
            'ajax' => true,
        ],
    ]
    

    (Note: Requires frontend JS to handle the response.)

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.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php