Skip to content

Basics of Menu Items

niknetniko edited this page Mar 5, 2016 · 10 revisions

Adding Items to Menus

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

Referencing Menu Items

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().

Get All Items

Menu::get('example')->all();

// or

$menu->all();

Get By Slug

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');

Get By ID

Menu::get('example')->find(1);

// or

$menu->find(1);

Get the First Item

Menu::get('example')->first();

// or

$menu->first();

Get the Last Item

Menu::get('example')->last();

// or

$menu->last();

Get All Active Items

Menu::get('example')->active();

// or

$menu->active();

Routing

Caffeinated Menus gives you various ways to link to your resources from your menus. Let's go over each method below.


URLs

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 adding your menu item:

...

$menu->add('Blog', 'blog');

...

Named Routes

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]]);

...

Controller Actions

You may also link directly to controller actions in a similar manner as named routes:

...

$menu->add('Blog', ['action' => 'BlogController@index']);

...

Named Route and Controller Action Parameters

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']]);

...

Sub-Items

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');
});