PHPackages                             hieu-le/laravel-menu - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hieu-le/laravel-menu

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hieu-le/laravel-menu
====================

Help to build menus easier in Laravel applications

1.0.2(10y ago)92.9k21MITPHPPHP &gt;=5.5.9

Since Nov 16Pushed 10y ago1 watchersCompare

[ Source](https://github.com/letrunghieu/laravel-menu)[ Packagist](https://packagist.org/packages/hieu-le/laravel-menu)[ RSS](/packages/hieu-le-laravel-menu/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (8)Versions (4)Used By (1)

Laravel Menu
============

[](#laravel-menu)

Help to build menus easier in Laravel applications (currently support Laravel 5 only)

[![Build Status](https://camo.githubusercontent.com/e9406e4082b040bb7cc9f70f78b63ad35db5477941dd655e550e697e02bd038f/68747470733a2f2f7472617669732d63692e6f72672f6c657472756e67686965752f6c61726176656c2d6d656e752e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/letrunghieu/laravel-menu)[![Latest Stable Version](https://camo.githubusercontent.com/faaec8476ec1793704f181a5b487e896b7f1e24d5d94ecaefcf6a24a72b85a30/68747470733a2f2f706f7365722e707567782e6f72672f686965752d6c652f6c61726176656c2d6d656e752f762f737461626c652e737667)](https://packagist.org/packages/hieu-le/laravel-menu)[![Code Climate](https://camo.githubusercontent.com/a61fc53692e6c2dd8b674c572c82ee85d1eed4fa8134bf6cb0b9af3a8e4cae80/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6c657472756e67686965752f6c61726176656c2d6d656e752f6261646765732f6770612e737667)](https://codeclimate.com/github/letrunghieu/laravel-menu)[![Test Coverage](https://camo.githubusercontent.com/5de20b8c7acaf96b5f62c415a903d158706b44560b6d983b17e4848adbb80fd1/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6c657472756e67686965752f6c61726176656c2d6d656e752f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/letrunghieu/laravel-menu/coverage)[![Total Downloads](https://camo.githubusercontent.com/7c6cab00ee052e67829c131bf209a86385707ac9e9c5feb7534ca9fca27ad598/68747470733a2f2f706f7365722e707567782e6f72672f686965752d6c652f6c61726176656c2d6d656e752f646f776e6c6f6164732e737667)](https://packagist.org/packages/hieu-le/laravel-menu)[![License](https://camo.githubusercontent.com/45ed6001250fd7586b73fc849e5619578e4b9632d1de70fb9ced488c1cc2f893/68747470733a2f2f706f7365722e707567782e6f72672f686965752d6c652f6c61726176656c2d6d656e752f6c6963656e73652e737667)](https://packagist.org/packages/hieu-le/laravel-menu)

Instalation
-----------

[](#instalation)

First, add this package to your project dependencies:

```
$> composer require "hieu-le/laravel-menu"

```

After Composer updated your vendors code, add the package service provider to your `providers` array in the `config/app.php` file:

```
HieuLe\LaravelMenu\LaravelMenuServiceProvider::class,

```

Now, you can access to the menu manager via `app('menu.manager')` object. If you want to use static methods via alias classes, register the package facade to your `aliases` array in the `config/app.php` file:

```
'Menu' => HieuLe\LaravelMenu\Facades\LaravelMenu::class,

```

Usage
-----

[](#usage)

You can create as many menus as you want in your application. A menu comes with a unique name which can be anything. You can get the menu instance via that name by `menu` method:

```

```

If there is no menu with the name you specified, a new one will be initiated and returned to you. Some menu comes with an optional *header* (or *label*), which can be set by the `setLabel` method **on the menu instance**.

```

```

The method return the menu instance itself to enable *method chaining*. Many menus can have the same label or don't have any label. Remember that you cannot retrieve a menu from menu manager via its label, you can only use its **name**.

I suggest two places to define your menus:

- a route middleware
- a service provider

### Add links to menu

[](#add-links-to-menu)

API: `$menu->addLink($text, array $url = [], $options = [])`

- `$text` is the anchor text
- `$url` is an **associative** array, which is used to generate the `href` attribute of the link. More details is explained later.
- `$option` array is described later.

If `$url` is an empty array, the `href` will be an hash character (`#`).

To assign an URL to the the link, pass a string to the `to` element of the `$url` array. Example: an item created with `$url` equals to `['to' => '/foo/bar']` has the `href` leading to `http://your-domain.com/foo/bar`, an item created with `$url` equals to `['to' => 'http://other-site.com/foo/bar']` has the `href` leading to `http://other-site.com/foo/bar`.

To assign an internal URL by your named route, pass the route name as string to the `route` element of the `$url` array. If the route has parameters, pass an array with the first element is the route name, the others are route parameters **and the appropriate parameter name as key**. For example:

```
