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

Knp Menu Laravel Package

knplabs/knp-menu

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require knplabs/knp-menu
    

    No additional configuration is required for basic usage.

  2. First Use Case: Create a simple menu in a controller or service:

    use Knp\Menu\MenuItem;
    use Knp\Menu\ItemInterface;
    
    $menu = new MenuItem('root');
    $menu->addChild('Home', ['route' => 'home']);
    $menu->addChild('About', ['route' => 'about']);
    return $menu;
    
  3. Rendering: Use Twig (recommended) or manually render the menu:

    {{ knp_menu_render(menu, { 'depth': 2, 'linkAttributes': { 'class': 'nav-link' } }) }}
    
  4. Key Files:

    • vendor/knplabs/knp-menu/src/ (Core classes)
    • Resources/views/ (Twig templates, if using Symfony)

Implementation Patterns

Common Workflows

  1. Dynamic Menu Building: Fetch menu items from a database and build the menu dynamically:

    $menu = $factory->createItem('root');
    foreach ($categories as $category) {
        $item = $menu->addChild($category->name, ['route' => 'category_show', 'parameters' => ['id' => $category->id]]);
        if ($category->hasChildren()) {
            $item->setAttribute('has_children', true);
        }
    }
    
  2. Menu Factories: Use MenuFactory to create reusable menu structures:

    $factory = new MenuFactory();
    $menu = $factory->createItem('main_menu');
    $menu->addChild('Dashboard', ['route' => 'dashboard']);
    $menu->addChild('Settings', ['route' => 'settings']);
    
  3. Twig Integration: Extend Twig with custom menu rendering:

    {% macro render_menu(menu, options) %}
        <ul class="menu">
            {% for item in menu %}
                <li>
                    {% if item.children|length > 0 %}
                        <a href="{{ path(item.route, item.parameters) }}">{{ item.label }}</a>
                        {{ render_menu(item.children, options) }}
                    {% else %}
                        <a href="{{ path(item.route, item.parameters) }}">{{ item.label }}</a>
                    {% endif %}
                </li>
            {% endfor %}
        </ul>
    {% endmacro %}
    
  4. Caching: Cache menus for performance:

    $menu = $factory->createItem('cached_menu');
    $menu->setCache($cache->getItem('menu_cache_key'));
    

Integration Tips

  • Symfony Integration: Use knp_menu.twig_extension in Twig configurations for seamless rendering.
  • Laravel Integration: Register a service provider to bind MenuFactory and MenuItem:
    $this->app->bind('knp-menu.factory', function () {
        return new MenuFactory();
    });
    
  • Route-Based Menus: Dynamically generate menus from route collections:
    $menu = $factory->createItem('route_menu');
    foreach ($router->getRouteCollection()->getByName('*') as $route) {
        $menu->addChild($route->getName(), ['route' => $route->getName()]);
    }
    

Gotchas and Tips

Pitfalls

  1. Circular References: Avoid adding the same child to multiple parents, which can cause infinite loops in rendering.

    // Bad: Circular reference
    $item1->addChild($item2);
    $item2->addChild($item1);
    
  2. Twig Autoescape: Ensure Twig autoescape is disabled for menu rendering to avoid HTML escaping issues:

    {{ knp_menu_render(menu, { 'autoescape': false }) }}
    
  3. Route Parameters: Incorrect route parameters can lead to broken links. Validate parameters before passing them to addChild:

    $item->addChild('Profile', ['route' => 'profile_show', 'parameters' => ['id' => $user->id]]);
    
  4. Menu Depth: Deeply nested menus can cause performance issues. Limit depth in rendering:

    {{ knp_menu_render(menu, { 'depth': 2 }) }}
    

Debugging

  • Enable Debug Mode: Use MenuItem::setCurrentUri() to highlight the current page in the menu:
    $menu->setCurrentUri(request()->getUri());
    
  • Check for Missing Routes: Ensure all routes referenced in the menu exist. Use Symfony’s Router to validate:
    if ($router->getRouteCollection()->get($routeName)) {
        $item->addChild('Link', ['route' => $routeName]);
    }
    

Configuration Quirks

  • Twig Extension: If using Laravel, manually register the Twig extension in your AppServiceProvider:
    $twig->addExtension(new \Knp\Menu\Twig\MenuExtension($menuFactory));
    
  • Custom Attributes: Use setAttribute() and getAttribute() to store custom data, but avoid overusing them for performance reasons.

Extension Points

  1. Custom Renderers: Extend MenuRenderer to create custom output formats (e.g., JSON for APIs):

    class JsonMenuRenderer extends MenuRenderer
    {
        public function render(ItemInterface $item, array $options)
        {
            return json_encode($item->toArray());
        }
    }
    
  2. Menu Builders: Create builder classes to encapsulate complex menu logic:

    class AdminMenuBuilder
    {
        public function build(MenuItem $menu)
        {
            $menu->addChild('Users', ['route' => 'admin_users']);
            $menu->addChild('Settings', ['route' => 'admin_settings']);
        }
    }
    
  3. Event Listeners: Use events (e.g., MenuItem::PRE_BUILD) to modify menus dynamically:

    $menu->addListener('PRE_BUILD', function (MenuItem $item) {
        if ($item->getName() === 'main_menu') {
            $item->addChild('Logout', ['route' => 'logout']);
        }
    });
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware