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 Laravel Package

21torr/menu

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer (if still needed for legacy projects):

    composer require 21torr/menu
    

    Publish the config (if available):

    php artisan vendor:publish --provider="21TORR\MenuBundle\MenuServiceProvider"
    
  2. Basic Usage Register a menu in a service provider (e.g., AppServiceProvider):

    use Symfony\Component\HttpKernel\Bundle\Bundle;
    use 21TORR\MenuBundle\Menu\MenuInterface;
    
    public function boot()
    {
        $this->app->bind('menu.main', function ($app) {
            return $this->app->makeWith('21TORR\MenuBundle\Menu\Menu', [
                'name' => 'main',
                'items' => [
                    ['label' => 'Home', 'route' => 'home'],
                    ['label' => 'About', 'route' => 'about'],
                ],
            ]);
        });
    }
    
  3. First Render Inject the menu into a controller or view:

    public function index(MenuInterface $menu)
    {
        return view('home', ['menu' => $menu->get('main')]);
    }
    

Implementation Patterns

Common Workflows

  1. Dynamic Menu Generation Use closures to fetch menu items dynamically (e.g., from a database):

    $this->app->bind('menu.admin', function ($app) {
        return $this->app->makeWith('21TORR\MenuBundle\Menu\Menu', [
            'name' => 'admin',
            'items' => function () use ($app) {
                return $app['db']->select('SELECT * FROM menu_items WHERE user_id = ?', [auth()->id()]);
            },
        ]);
    });
    
  2. Nested Menus Support hierarchical menus via recursive item definitions:

    'items' => [
        [
            'label' => 'Products',
            'route' => 'products.index',
            'children' => [
                ['label' => 'Category 1', 'route' => 'products.category1'],
            ],
        ],
    ],
    
  3. View Integration Render menus in Blade templates:

    @foreach($menu->get('main') as $item)
        <li>
            <a href="{{ route($item['route']) }}">{{ $item['label'] }}</a>
            @if(isset($item['children']))
                <ul>
                    @include('partials.menu', ['items' => $item['children']])
                </ul>
            @endif
        </li>
    @endforeach
    
  4. Active Item Highlighting Add logic to highlight the current route:

    'items' => array_map(function ($item) {
        $item['active'] = request()->routeIs($item['route']);
        return $item;
    }, $originalItems),
    

Gotchas and Tips

Pitfalls

  1. Archived Status

  2. Lack of Documentation

    • No official docs beyond the README. Reverse-engineer usage from tests or examples.
    • Key classes: Menu, MenuInterface, MenuItem.
  3. Service Provider Binding

    • Menus must be explicitly bound in a provider. No auto-discovery.
    • Override existing bindings carefully to avoid conflicts.
  4. No Built-in Caching

    • Dynamic menus (e.g., database-driven) may cause performance issues.
    • Cache manually:
      $menu = Cache::remember("menu.{$userId}", now()->addHours(1), function () use ($userId) {
          return $this->app->make('menu.admin');
      });
      

Debugging Tips

  1. Check Bindings Dump registered menus:

    dd($this->app->bound('menu.*')); // Check if menus are bound
    dd($this->app->make('menu.main')); // Inspect a menu instance
    
  2. Validate Item Structure Ensure items are arrays with label and route keys. Missing keys may cause silent failures.

  3. Route Resolution Debug route generation:

    dd(route($item['route'])); // Verify route exists
    

Extension Points

  1. Custom Menu Items Extend MenuItem to add metadata (e.g., icons, permissions):

    class ExtendedMenuItem extends MenuItem
    {
        public $icon;
        public $requiresPermission;
    }
    
  2. Menu Builders Create a fluent interface for menu construction:

    class MenuBuilder
    {
        public function addItem(string $label, string $route): self
        {
            $this->items[] = compact('label', 'route');
            return $this;
        }
    }
    
  3. Event Listeners Trigger events for menu rendering (e.g., MenuRendering):

    event(new MenuRendering($menuName, $items));
    
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle