|
| 1 | +<p align="center"><img src="https://i.ibb.co/31yzFSw/logo.png"></p> |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://packagist.org/packages/hexadog/laravel-menus-manager"> |
| 5 | + <img src="https://poser.pugx.org/hexadog/laravel-menus-manager/v" alt="Latest Stable Version"> |
| 6 | + </a> |
| 7 | + <a href="https://packagist.org/packages/hexadog/laravel-menus-manager"> |
| 8 | + <img src="https://poser.pugx.org/hexadog/laravel-menus-manager/downloads" alt="Total Downloads"> |
| 9 | + </a> |
| 10 | + <a href="https://packagist.org/packages/hexadog/laravel-themes-manager"> |
| 11 | + <img src="https://poser.pugx.org/hexadog/laravel-menus-manager/license" alt="License"> |
| 12 | + </a> |
| 13 | +</p> |
| 14 | + |
| 15 | +<!-- omit in toc --> |
| 16 | +## Introduction |
| 17 | +<code>hexadog/laravel-menus-manager</code> is a Laravel package to ease dynamic menus management. |
| 18 | + |
| 19 | +<!-- omit in toc --> |
| 20 | +## Installation |
| 21 | +This package requires PHP 7.3 and Laravel 7.0 or higher. |
| 22 | + |
| 23 | +To get started, install Menus Manager using Composer: |
| 24 | +```shell |
| 25 | +composer require hexadog/laravel-menus-manager |
| 26 | +``` |
| 27 | + |
| 28 | +The package will automatically register its service provider. |
| 29 | + |
| 30 | +<!-- omit in toc --> |
| 31 | +## Usage |
| 32 | +Menus Manager has many features to help you working with dynamic menus |
| 33 | + |
| 34 | +- [Create Menu](#create-menu) |
| 35 | +- [Menu hierarchy](#menu-hierarchy) |
| 36 | +- [Menu Item](#menu-item) |
| 37 | + - [Item Types](#item-types) |
| 38 | + - [Route item](#route-item) |
| 39 | + - [Url item](#url-item) |
| 40 | + - [Divider item](#divider-item) |
| 41 | + - [Header item](#header-item) |
| 42 | + - [Item Icon](#item-icon) |
| 43 | + - [Item Order](#item-order) |
| 44 | + - [Item Visibility](#item-visibility) |
| 45 | + - [Active state](#active-state) |
| 46 | +- [Search for item](#search-for-item) |
| 47 | +- [Menu Tree](#menu-tree) |
| 48 | +- [Blade components](#blade-components) |
| 49 | + |
| 50 | +### Create Menu |
| 51 | +Use the provided `Menus` facade with `register` method to create a new menu. |
| 52 | +_**notice:** if menu with given name already exists it will be returned - this way you can access to any existing menu anywhere in your application_ |
| 53 | +```php |
| 54 | +// Create new "main" Menu |
| 55 | +$menu = Menus::register('main'); |
| 56 | +``` |
| 57 | + |
| 58 | +You can now access to the menu any time using: |
| 59 | +```php |
| 60 | +// Retreive "main" Menu |
| 61 | +$menu = Menus::get('main'); |
| 62 | +``` |
| 63 | + |
| 64 | +And get all items for the Menu |
| 65 | +```php |
| 66 | +// Get all items from Menu |
| 67 | +$items = $menu->items(); |
| 68 | +``` |
| 69 | +You can get a specific item by using search methods. See [Search an item](#search-an-item). |
| 70 | + |
| 71 | +### Menu hierarchy |
| 72 | +Menus Manager lets you create multi-level menus. Each Menu item can have as many children as you want. See [Menu Item](#menu-item) to find out how to create a new Menu Item. |
| 73 | + |
| 74 | +You can easily retreive declared children using: |
| 75 | +```php |
| 76 | +// Check if item has children |
| 77 | +if ($menuItem->hasChildren()) { |
| 78 | + // Get all item children as Collection |
| 79 | + $items = $menuItem->children(); |
| 80 | + |
| 81 | + // Loop into items |
| 82 | + foreach($items as $item) { |
| 83 | + // Get item parent |
| 84 | + $parent = $item->parent(); |
| 85 | + |
| 86 | + // ... |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Menu Item |
| 92 | +An item can be added at any level: the menu itself or any child item. |
| 93 | +```php |
| 94 | +// Create a new menu |
| 95 | +$menu = Menus::register('main'); |
| 96 | + |
| 97 | +// Add a first-level item |
| 98 | +$menu->route('index', 'Home'); |
| 99 | + |
| 100 | +// Create a first-level item with children |
| 101 | +$menuItem = $menu->header('Our packages'); |
| 102 | +$menuItem->url('https://github.com/hexadog/laravel-menus-manager', 'Laravel Menus Manager')->order(1); |
| 103 | +$menuItem->url('https://github.com/hexadog/laravel-themes-manager', 'Laravel Themes Manager')->order(3); |
| 104 | +$menuItem->url('https://github.com/hexadog/laravel-theme-installer', 'Laravel Theme Installer')->order(2); |
| 105 | + |
| 106 | +// Create first-level items with visibility condition |
| 107 | +$menu->route('profile.show', __('Profile'))->if(Auth()->check()); |
| 108 | +$menu->route('login', __('Login'))->if(!Auth()->check()); |
| 109 | +``` |
| 110 | + |
| 111 | +You can access to the generated item url with `getUrl()` method on any item. |
| 112 | +```php |
| 113 | +$menuItem->getUrl(); |
| 114 | +``` |
| 115 | + |
| 116 | +#### Item Types |
| 117 | +Menus Manager handle multiple item types: Route item, URL item, Header item and Divider item |
| 118 | + |
| 119 | +##### Route item |
| 120 | +Add a menu item for a route by passing the route name and a title |
| 121 | +```php |
| 122 | +$menuItem = $menu->route('index', 'Home'); |
| 123 | +``` |
| 124 | + |
| 125 | +You can pass parameters to the route by passing an array instead of a string as first parameter |
| 126 | +```php |
| 127 | +$menuItem = $menu->route(['index', ['type' => 'anonymous']], 'Home'); |
| 128 | +``` |
| 129 | + |
| 130 | +##### Url item |
| 131 | +Add a menu item for an URL with the given title |
| 132 | +```php |
| 133 | +$menuItem = $menu->url('https://hexadog.com', 'hexadog'); |
| 134 | +``` |
| 135 | + |
| 136 | +##### Divider item |
| 137 | +A simple divider: no action available. No title required for this type of item. |
| 138 | +```php |
| 139 | +$menuItem = $menu->divider(); |
| 140 | +``` |
| 141 | + |
| 142 | +##### Header item |
| 143 | +Add header item: no action available. Mainly used to visually group sub-menus |
| 144 | +```php |
| 145 | +$menuItem = $menu->header('General'); |
| 146 | +``` |
| 147 | + |
| 148 | +#### Item Icon |
| 149 | +You can add an icon to your menu item by calling `icon()` method with the icon classes as parameter. |
| 150 | +```php |
| 151 | +$menu->route('index', 'Home')->icon('fas fa-home'); |
| 152 | +``` |
| 153 | + |
| 154 | +#### Item Order |
| 155 | +By default items are displayed in order they are created. You can specify item order to organize your menu entries: |
| 156 | +```php |
| 157 | +$menu->route('index', 'Home')->order(1); |
| 158 | +$menu->route('contact', 'Contact')->order(2); |
| 159 | +``` |
| 160 | + |
| 161 | +#### Item Visibility |
| 162 | +```php |
| 163 | +$menuItem->isVisible(); |
| 164 | +``` |
| 165 | + |
| 166 | +You can condition item visibility by using `if()` method. |
| 167 | +You can chain conditions. This way each condition must be filled to make the item visible. |
| 168 | +```php |
| 169 | +$menu->route('profile.show', __('Profile'))->if(Auth()->check()); |
| 170 | +$menu->route('login', __('Login'))->if(!Auth()->check()); |
| 171 | + |
| 172 | +$menu->route('post.create', __('New Post'))->if(Auth()->check())->if(Auth()->user()->can('create.post')); |
| 173 | +``` |
| 174 | + |
| 175 | +#### Active state |
| 176 | +Check if current item is active or has an active child. |
| 177 | +Depending on the item type, active state is determined using `Request::is()` or `Route::is()` Laravel methods. |
| 178 | +```php |
| 179 | +$menuItem->isActive(); |
| 180 | +``` |
| 181 | + |
| 182 | +### Search for item |
| 183 | +Search an item recursively (in all hierarchy). |
| 184 | +```php |
| 185 | +$menu->search('title', 'Home'); // Return the found item or null |
| 186 | +``` |
| 187 | + |
| 188 | +or search in first level item children |
| 189 | +```php |
| 190 | +$menu->findBy('title', 'Home'); |
| 191 | +``` |
| 192 | + |
| 193 | +You can search an item by title and add it if not found in one line with `findByTitleOrAdd()` helper method. |
| 194 | +```php |
| 195 | +$menu->findByTitleOrAdd('title'); |
| 196 | +``` |
| 197 | + |
| 198 | +### Menu Tree |
| 199 | +Menu and Item implement `Illuminate\Contracts\Support\Arrayable` interface. Calling `toArray()` method on the follwing menu: |
| 200 | +```php |
| 201 | +// Create a new menu |
| 202 | +$menu = Menus::register('main'); |
| 203 | + |
| 204 | +// Add a first-level item |
| 205 | +$menu->route('index', 'Home'); |
| 206 | + |
| 207 | +// Create a first-level item with children |
| 208 | +$menuItem = $menu->header('Our packages'); |
| 209 | +$menuItem->url('https://github.com/hexadog/laravel-menus-manager', 'Laravel Menus Manager')->order(1); |
| 210 | +$menuItem->url('https://github.com/hexadog/laravel-themes-manager', 'Laravel Themes Manager')->order(3); |
| 211 | +$menuItem->url('https://github.com/hexadog/laravel-theme-installer', 'Laravel Theme Installer')->order(2); |
| 212 | + |
| 213 | +// Create first-level items with visibility condition |
| 214 | +$menu->route('profile.show', __('Profile'))->if(Auth()->check()); |
| 215 | +$menu->route('login', __('Login'))->if(!Auth()->check()); |
| 216 | + |
| 217 | +// Get Menu Tree |
| 218 | +$menu->toArray(); |
| 219 | +``` |
| 220 | + |
| 221 | +Returns an array of menu content |
| 222 | +```php |
| 223 | +[ |
| 224 | + "name" => "main", |
| 225 | + "items" => [ |
| 226 | + 0 => [ |
| 227 | + "attributes" => [ |
| 228 | + "id" => "id-5f8c4a3d803dd817648152" |
| 229 | + ], |
| 230 | + "active" => true, |
| 231 | + "children" => [], |
| 232 | + "icon" => null, |
| 233 | + "order" => 0, |
| 234 | + "title" => "Home", |
| 235 | + "type" => "link", |
| 236 | + "url" => "http://127.0.0.1:8000", |
| 237 | + ], |
| 238 | + 1 => [ |
| 239 | + "attributes" => [ |
| 240 | + "id" => "id-5f8c4a3d8045f366812051" |
| 241 | + ], |
| 242 | + "active" => false, |
| 243 | + "children" => [ |
| 244 | + 0 => [ |
| 245 | + "attributes" => [ |
| 246 | + "id" => "id-5f8c4a3d80476901878768" |
| 247 | + ], |
| 248 | + "active" => false, |
| 249 | + "children" => [], |
| 250 | + "icon" => null, |
| 251 | + "order" => 1, |
| 252 | + "title" => "Laravel Menus Manager", |
| 253 | + "type" => "link", |
| 254 | + "url" => "https://github.com/hexadog/laravel-menus-manager", |
| 255 | + ], |
| 256 | + 2 => [ |
| 257 | + "attributes" => [ |
| 258 | + "id" => "id-5f8c4a3d80496954609369" |
| 259 | + ], |
| 260 | + "active" => false, |
| 261 | + "children" => [], |
| 262 | + "icon" => null, |
| 263 | + "order" => 2, |
| 264 | + "title" => "Laravel Theme Installer", |
| 265 | + "type" => "link", |
| 266 | + "url" => "https://github.com/hexadog/laravel-theme-installer", |
| 267 | + ], |
| 268 | + 1 => [ |
| 269 | + "attributes" => [ |
| 270 | + "id" => "id-5f8c4a3d8048e808061014" |
| 271 | + ], |
| 272 | + "active" => false, |
| 273 | + "children" => [], |
| 274 | + "icon" => null, |
| 275 | + "order" => 3, |
| 276 | + "title" => "Laravel Themes Manager", |
| 277 | + "type" => "link", |
| 278 | + "url" => "https://github.com/hexadog/laravel-themes-manager", |
| 279 | + ] |
| 280 | + ], |
| 281 | + "icon" => null, |
| 282 | + "order" => 0, |
| 283 | + "title" => "Our packages", |
| 284 | + "type" => "header", |
| 285 | + "url" => "", |
| 286 | + ] |
| 287 | + ] |
| 288 | +] |
| 289 | +``` |
| 290 | + |
| 291 | +### Blade components |
| 292 | +Menus Manager provides blade components to ease integration in your designs. |
| 293 | + |
| 294 | +You just have to pass your Menu name to get full render. |
| 295 | +```php |
| 296 | +<x-menus-menu name="main" /> |
| 297 | +``` |
| 298 | +It uses `x-menus-children`, `x-menus-divider`, `x-menus-header`, `x-menus-icon` and `x-menus-item` dedicated components. By default Menus Manager will scaffold your application's integration with the [Tailwind CSS](https://tailwindcss.com/) and [Alpine.js](https://github.com/alpinejs/alpine). |
| 299 | + |
| 300 | +You can customize component's views to fit to your need by publishing them into your application resources: |
| 301 | +```shell |
| 302 | +php artisan vendor:publish --provider="Hexadog\MenusManager\Providers\PackageServiceProvider" |
| 303 | +``` |
| 304 | + |
| 305 | +<!-- omit in toc --> |
| 306 | +## Credits |
| 307 | +- Logo made by [DesignEvo free logo creator](https://www.designevo.com/logo-maker/) |
| 308 | + |
| 309 | +<!-- omit in toc --> |
| 310 | +## License |
| 311 | +Laravel Menus Manager is open-sourced software licensed under the [MIT license](LICENSE). |
0 commit comments