-
Notifications
You must be signed in to change notification settings - Fork 59
Basics of Menu Items
Add menu items through the add()
method:
Menu::make('example', function(Builder $menu) {
$menu->add('Home', '/');
$menu->add('About', '/about');
$menu->add('Blog', '/blog');
$menu->add('Contact Me', '/contact-me');
});
When a menu item is added, it'll automatically be assigned both a slug
and ID
that it can be referenced by in the future if the need arises. The slug
is determined by the title that you assign an item, in camel case form. So the above menu items would have the following slugs:
Item Title | Item Slug |
---|---|
Home | home |
About | about |
Blog | blog |
Contact Me | contactMe |
Let's look at how we can reference our menu items below! These methods can be used both within the context of Menu::make()
as well as outside via Menu::get()
.
Menu::get('example')->all();
// or
$menu->all();
The get()
method has a facade of item()
as well, to make it more readable if referencing it directly from Menu::get()
. They both achieve the same thing.
Menu::get('example')->item('contactMe');
// or
$menu->item('contactMe');
// or
$menu->get('contactMe');
Menu::get('example')->find(1);
// or
$menu->find(1);
Menu::get('example')->first();
// or
$menu->first();
Menu::get('example')->last();
// or
$menu->last();
Menu::get('example')->active();
// or
$menu->active();
Caffeinated Menus gives you various ways to link to your resources from your menus. Let's go over each method below.
This is as easy and basic as it goes. You may simply assign a URL to your menu item by passing the URI as the second parameter when add
ing your menu item:
...
$menu->add('Blog', 'blog');
...
If you make use of the named routes functionality of Laravel, why not do the same for your menu items as well? Simply pass an array referencing the route in the second parameter, like so:
...
$menu->add('Blog', ['route' => 'blog.page']);
...
If your need to pass parameters to the route Simply pass an array to the route parameter, like so:
...
$menu->add('Blog', ['route' => ['blog.page', 'id' => 56]]);
...
You may also link directly to controller actions in a similar manner as named routes:
...
$menu->add('Blog', ['action' => 'BlogController@index']);
...
If you need to pass along additional parameters to either the named routes or controller actions methods, simply utilize an array in place of a string like so:
...
// Named Route
$menu->add('Blog', ['route' => ['blog.page', 'slug' => 'lorem-ipsum-dolor']]);
// Controller Action
$menu->add('Blog', ['action' => ['BlogController@index', 'slug' => 'lorem-ipsum-dolor']]);
...
Menu items can have as many child items as you need, and it's super simple to do.
Menu::make('example', function(Builder $menu) {
$menu->add('Espresso', '/espresso');
$menu->espresso->add('Americano', '/espresso/americano');
$menu->espresso->add('Breve', '/espresso/breve');
$menu->espresso->add('Cappuccino', '/espresso/cappuccino');
$menu->espresso->add('Latte', '/espresso/latte');
});
You may also assign menu items to a variable when adding them, and reference them that way. This makes it possible to add sub-items to items in other languages.
Menu::make('example', function(Builder $menu) {
$profile = $menu->add('プロフィール', '#');
$profile->add('プロファイル編集', 'profile/edit');
});
🔴 Best Temporary Email - https://1secmail.ru 🔴