diff --git a/README.md b/README.md index ee516d2..f4621d5 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,8 @@ function generate() $this->menu->add('Edit Myself', 'route:users.edit|id:5'); // Method 5 $this->menu->add('Logout')->route('auth.logout'); // Method 6 $this->menu->add('Some Other Link', ['url' => '/link']); // Method 7 + $this->menu->add('Sub Menu', 'menu:menu_name); // Method 8 + $this->menu->add('Sub Menu', 'sub-menu:sub_menu_name); // Method 9 } ``` @@ -131,6 +133,8 @@ function generate() | 5 | The `$this->menu` instance | The route is associated with arguments, useful to create a quick route with parameters. | | 6 | A new `MenuItem` | The route is assigned using the fluent interface, which makes the code look cleaner. You can also add an array of route parameters as the second argument. | | 7 | The `$this->menu` instance | You can pass an array as the second argument to immediately issue all the variables to the menu item, when you don't want to use the fluent interface. | +| 8 | The `$this->menu` instance | You can pass the name of another menu and the class will merge that menu with this menu. | +| 9 | The `$this->menu` instance | You can pass the name of another menu and the class will pull in that menu as a sub menu. | @@ -285,4 +289,4 @@ This should achieve a menu looking like [the default bootstrap navbar](https://g # Tips -- The `__construct` of your menu class works with dependency injection. Simply type hint the stuff you need in the parameter list and we'll ask Laravel to inject the stuff you need. \ No newline at end of file +- The `__construct` of your menu class works with dependency injection. Simply type hint the stuff you need in the parameter list and we'll ask Laravel to inject the stuff you need. diff --git a/src/CreatesMenuItems.php b/src/CreatesMenuItems.php index c762381..7fb1f19 100644 --- a/src/CreatesMenuItems.php +++ b/src/CreatesMenuItems.php @@ -93,6 +93,28 @@ public function &add(string $title, $config = null) return $this; } + + + // Is it another Menu? + if(starts_with($config, 'menu:') || starts_with($config, 'sub-menu:')) { + $slug = $item['__slug__'] = snake_case($title); + + if(starts_with($config, 'menu:')) { + $items =& $this->_items; + $menu = app('menu')->get(str_replace_first('menu:', '', $config)); + } else {// if(starts_with($config, 'sub-menu:')) { + $items =& $item->_items; + $menu = app('menu')->get(str_replace_first('sub-menu:', '', $config)); + } + + $items = array_merge($items, $menu->_items); + + if(starts_with($config, 'sub-menu:')) { + $this->_items[$slug] = &$item; + } + + return $this; + } // Else it's a plain url $this->_items[snake_case($title)] = &$item; @@ -100,4 +122,4 @@ public function &add(string $title, $config = null) return $this; } -} \ No newline at end of file +}