|
| 1 | +# Laravel Menu Wrapper |
| 2 | + |
| 3 | +Laravel Menu Wrapper is a lightweight package that helps you manage dynamic menus in your Laravel app with less effort. |
| 4 | + |
| 5 | +> Simply put, this package automatically generates an array of menus based on what you define. |
| 6 | +
|
| 7 | +Tested on Laravel `10.x`, `11.x`, and `12.x` — works successfully without any issues. |
| 8 | + |
| 9 | +# Installation |
| 10 | + |
| 11 | +Using Composer (recommended): |
| 12 | + |
| 13 | +```sh |
| 14 | +cd your-laravel-project |
| 15 | +composer require sukristyan/laravel-menu-wrapper |
| 16 | +``` |
| 17 | + |
| 18 | +After the installation is complete, the provider and configuration files will be published automatically. |
| 19 | + |
| 20 | +# Configuration |
| 21 | + |
| 22 | +Currently, there are only 2 configuration items. |
| 23 | + |
| 24 | +## Group As (`group_as`) |
| 25 | + |
| 26 | +Values: `item` or `key`. |
| 27 | + |
| 28 | +This setting determines how your menus will be grouped. |
| 29 | + |
| 30 | +`key` means that your group label will be used as the array key. Your defined menu will look like this: |
| 31 | + |
| 32 | +```php |
| 33 | +[ |
| 34 | + 'Group Name' => [ |
| 35 | + [ |
| 36 | + 'label' => 'Menu #1', |
| 37 | + ], |
| 38 | + [ |
| 39 | + 'label' => 'Menu #2', |
| 40 | + ] |
| 41 | + ], |
| 42 | + ... |
| 43 | +] |
| 44 | +``` |
| 45 | + |
| 46 | +`item` means that your group label will be included in the array as `group_name`, and your defined menus will be inserted under `childs`. Your menu will look like this: |
| 47 | + |
| 48 | +```php |
| 49 | +[ |
| 50 | + [ |
| 51 | + 'group_name' => 'Group Name', |
| 52 | + 'childs' => [ |
| 53 | + [ |
| 54 | + 'label' => 'Menu #1', |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'label' => 'Menu #2', |
| 58 | + ], |
| 59 | + ], |
| 60 | + ], |
| 61 | + ... |
| 62 | +] |
| 63 | +``` |
| 64 | + |
| 65 | +## Populated Items (`populated_items`) |
| 66 | + |
| 67 | +Here, you can define what you want to collect from the 'Illuminate\Routing\Route', or add custom identifiers to help integrate the menu into your application. |
| 68 | + |
| 69 | +```php |
| 70 | +'populate_items' => function (Route $route) { |
| 71 | + return [ |
| 72 | + 'route_name' => $route->getName(), |
| 73 | + 'type' => 'children' |
| 74 | + ]; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +# Usage |
| 79 | + |
| 80 | +> NOTE: All collected data will follow what you define in `config/laravel-menu-wrapper.php`. |
| 81 | +
|
| 82 | +Go to your 'routes/web.php', and add the `menu('Menu Label', 'Group Label')` function to your route: |
| 83 | + |
| 84 | +```php |
| 85 | +Route::get('index', [DashboardController::class, 'index']) |
| 86 | + ->name('index') |
| 87 | + ->menu('Dashboard'); |
| 88 | +``` |
| 89 | + |
| 90 | +After that, you can run `php artisan menu:list` to show all your menus. You can also get all menus using this code: |
| 91 | + |
| 92 | +```php |
| 93 | +app('sukristyan.menu')->all(); |
| 94 | +``` |
| 95 | + |
| 96 | +You can parse the resulting menu array and use it anywhere in your project. |
| 97 | + |
| 98 | +You can directly use it in a `foreach` loop, or create a custom page to manage the menu — allowing administrators to add or remove menu items for users. |
| 99 | + |
| 100 | +```php |
| 101 | +$menus = app('sukristyan.menu')->all(); |
| 102 | + |
| 103 | +foreach ($menus as $menu) { |
| 104 | + ... |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +# License |
| 109 | + |
| 110 | +The `sukristyan/laravel-menu-wrapper` package is open-sourced software licensed under the [MIT license](https://github.com/sukristyan/laravel-menu-wrapper/blob/master/LICENSE). |
0 commit comments