Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot closing single-quote

$this->menu->add('Sub Menu', 'sub-menu:sub_menu_name); // Method 9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot closing single-quote


}
```
Expand All @@ -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. |



Expand Down Expand Up @@ -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.
- 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.
24 changes: 23 additions & 1 deletion src/CreatesMenuItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,33 @@ public function &add(string $title, $config = null)

return $this;
}


// Is it another Menu?
if(starts_with($config, 'menu:') || starts_with($config, 'sub-menu:')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after if

$slug = $item['__slug__'] = snake_case($title);

if(starts_with($config, 'menu:')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after if

$items =& $this->_items;
$menu = app('menu')->get(str_replace_first('menu:', '', $config));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should either return $this (soft failure) or throw exception if menu doesn't exist.

} else {// if(starts_with($config, 'sub-menu:')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the comment containing logical code

$items =& $item->_items;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$item->_items is a protected item and it should stay that way. I'd suggest moving the functionality away from the add method and move it to a merge method under the trait CreatesMenuItems.

$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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation. Make sure you use spaces (4) instead of tabs.


// Else it's a plain url
$this->_items[snake_case($title)] = &$item;
$item->url = $config;

return $this;
}
}
}