Download the PHP package elmdash/menu without Composer
On this page you can find all versions of the php package elmdash/menu. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package menu
ElmDash Menu
Menus are but a simple tree structure. You can implement them a million ways, but this is a clean and minimal menu structure that handles the basic chores of setting active states and toggling menu items depending on a user's authorization within Laravel apps.
In your provider's boot method add something like:
Then in your view:
Labels are set in lang/menu.php
:
Notice that the translation keys are the menu items' route names but with dashes instead of periods. Also, notice that the top-level name of the menu is used to group the translations.
Features
Root Menus
Top-level (root) menu objects will usually not be rendered and won't have a route to reference. You can give these routes names for convenience.
Adding items
The menu tree is made up of only Menu
objects so every item in the tree has the same methods as every other item. That is, there is not "MenuRoot", "MenuGroup", or "MenuItem" differentiation.
Adding an item can be as simple as adding the name of a route. (At this time, you can't add any routes that aren't named.)
This returns the newly created Menu
child. You can use this for chaining.
It may be easier for several reasons, however, to use a callback to create your tree structures. First, it's more visually appealing and second, the functions are called once needed. This is handy for delaying menu initialization code until everything else has been initialized.
There are other means of adding items:
Routes
The route name is typically specified when adding the menu item.
If your route takes parameters, you should add them to the menu. They will be used to render the correct href value and for determining the active menu item.
The params used to find a match are just the params defined on the route. To add finer control, you can extract these params yourself. The result is cached so it will only get called once. For example, you may need to get a param value from a different route parameter (like during an edit route).
Getting the URL to render in your view is simply like this:
Or if you need absolute URLs
Active menu items
When a child item is active, all parents will be considered active. To find out which menu item matches the currently active URL, we compare the current route's name with each menu item's route name. For example, the "users.account" menu item will be active when the "users.account" route is active. It's just a strict name (and possibly parameters) comparison.
However, you may have more than one view per your lowest level of menu items. You'll need a way to specify other named routes that should mark a menu item as active. For that we have globbing:
Again, we're only working with named routes, so make sure your routes are all named.
As shown above, to find out if an item is active in your view just call item.isActive
.
For submenus, you may also need to get the currently active item of some root menu object.
Access
Menu items can be hidden for users who don't have permission. This just uses Laravel's authorization policies.
Menu items that are not visible to the current user will not be returned by the menu.children
function. If a parent item is not visible, then the children will also not be marked as visible. In submenus, you may need to check for visibility directly (like, if you are not looping directly over the results of menu.children
.
By default, all menu items require authentication. To create menu items for anonymous users:
Flags
You can add flags to a menu item for inquiring about menu items later.
Labels
Labels are just taken from the Laravel's translation infrastructure.
By default, the translation key is just the route name provided when adding the menu item, but with the dots converted to dashes
If the top-level menu object has a name, then that will be prefixed to the translation key: i.e. 'top-left' and 'my.route.name' => 'top-left.my-route-name'.
Group names are also prefixed
At any place up the menu tree, you may specify a translation namespace.
You can override the language key to use:
Or you can override the label directly