PHPackages                             tlr/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. tlr/menu

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

tlr/menu
========

Take some of the stress and boilerplate out of building menus (or indeed any list, because that's basically what a menu is) With support for laravel

v3.0.7(11y ago)189.7k1[3 issues](https://github.com/tedslittlerobot/menu-builder/issues)MITPHPPHP &gt;=5.3.0

Since Nov 25Pushed 11y ago3 watchersCompare

[ Source](https://github.com/tedslittlerobot/menu-builder)[ Packagist](https://packagist.org/packages/tlr/menu)[ RSS](/packages/tlr-menu/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (21)Used By (0)

Menu Builder
============

[](#menu-builder)

> An attempt to take a bit of the stress and boilerplate out of building menus (or indeed any list, because that's basically what a menu is) Comes with support for Laravel 4 (other frameworks to follow)

- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Managing Multiple Menus](#managing-multiple-menus)
- [Filtering Menus](#filters)
- [Activating Menu Items](#activating)

###### Framework Integrations:

[](#framework-integrations)

- [Laravel Integration](#laravel)

### Installation

[](#installation)

Simply require the library like so:

```
composer require tlr/menu "2.*"
```

### Basic Usage

[](#basic-usage)

Make a new `MenuItem` class for your menu:

```
$menu = new Tlr\Menu\MenuItem;
```

Add some menu items:

```
$home = $menu->item( 'home', 'Home', 'http://foo.com' );
$blog = $menu->item( 'blog', 'Blog', 'http://foo.com/blog' );
$about = $menu->item( 'about', 'About', 'http://foo.com/about-us' );
```

Why not add a sub-menu? The menu, and all of its items are all the same `MenuItem` class, so creating submenus is the same as with top level menus:

```
$about->item( 'where-we-are', 'Where We Are', 'http://eric.com/where-are-we' );
$about->item( 'contact-us', 'Contact Us', 'http://eric.com/contact-us' );
```

Then in you view, you can iterate over the menu's items using the `$menu->getItems()` method.

You can retreive an existing item by using its key:

```
$blog = $menu->item('blog');
```

The method signature is as follows:

```
$menu->item( $key[, $title = "", $options = array(), $attributes = array(), $index = $n + 100] )

```

- `$key` is a string key used to retrieve the item. It is also added, in slugified form, to the class attribute. It is the only required argument.
- `$title` is the text to display in the list item
- `$options` is an array of options for the item. If you pass a string to the third item, it will assume it is the link option, and will convert it to `array( 'link' => $string )`
- `$attributes` is an array of attributes for the HTML element
- `$index` is an optional index to insert a new menu item at. The indices, by default, default to increments of 100, starting at 100, so you can easily insert items in between them.

### Managing Multiple Menus

[](#managing-multiple-menus)

If you have, say, a header and a footer menu, you can use the MenuRepository class:

```
$repo = new MenuRepository;

$headerMenu = $repo->menu( 'header-nav' );
$footerMenu = $repo->menu( 'footer-nav' );
```

and you can retrieve the menu later in your code in the same way:

```
$headerMenu = $repo->menu( 'header-nav' ); // will use the existing menu instance cached with this key
$newMenu = $repo->menu( 'other-nav' ); // that key hasn't been used yet, so a new instance will be created and cached with that key
```

### Filters

[](#filters)

The menu can be filtered. Say you have a menu for usage in an admin area, or in a context with user auth levels or permissions. You have two options:

- You can pass a filter closure to the `getItems` method that fill be used to filter the menu items.

```
// This will filter the items based on user permissions
$menu->getItems(function($item) use ($user)
{
	return $user->can( $item->option( 'permissions', array() ) );
});
```

- You can add multiple filters to a menu with the `addFilter($callable)` method. These filters will be applied when the `getItems` method is called.

```
// This will add a filter that only lets the given user see the menu items if they have the appropriate auth level
$menu->addFilter(function($item) use ($user)
{
	return $item->option('auth') authLevel;
});
$menu->getItems();
```

- You don't have to filter an entire menu - you can filter a submenu, too:

```
$about->addFilter(function()
{
	return $item->isVisible();
});
```

- By default, any filters added with `addFilter` get applied to submenus. You can override this behaviour by passing false as the second argument:

```
$menu->addFilter(function($item) use ($user)
{
	return $user->canSeePage( $item );
}, false);
```

- If you do not want to filter the items, you can call `$menu->getItems(false)`

### Activating

[](#activating)

To mark menu items as active, you have a few options:

Say you have this 2 level menu:

```
$home = $menu->item( 'home', 'Home', 'http://foo.com' );
$blog = $menu->item( 'blog', 'Blog', 'http://foo.com/blog' );
$about = $menu->item( 'about', 'About', 'http://foo.com/about-us' );
    $contact = $about->item( 'contact', 'Contact Us', 'http://foo.com/contact-us' );
    $find = $about->item( 'find', 'Find Us', 'http://foo.com/find-us' );
```

##### Manual Activation

[](#manual-activation)

You can manually activate any of those items with the setActive method:

```
$blog->setActive();
```

##### URL Matching

[](#url-matching)

For a more automated approach, you can recursively mark one of those as activated based on the current URL. For example:

```
$menu->activate( $currentUrl );
```

This would match the given url against each of the menu's items, and mark them as active if the url matches. It also calls each item's children, and marks the parents as active if they have an active child item. ie. activeness bubbles up the chain.

So, if the current URL was `http://foo.com/contact-us`, this would mark the `about` menu item, and its child item, `contact` as active.

##### Advanced

[](#advanced)

If you want to match based on something different than URL, you can match against anything in a `MenuItem`'s `options` array:

```
$home = $menu->item( 'home', 'Home', [ 'link' => 'http://foo.com', 'routename' => 'home' ] );
$blog = $menu->item( 'blog', 'Blog', [ 'link' => 'http://foo.com/blog', 'routename' => 'blog' ] );

$menu->activate( 'blog', 'routename' );
```

### Laravel

[](#laravel)

Add `Tlr\Menu\Laravel\MenuServiceProvider` to the `providers` array in Laravel's `config/app.php`'s, and add `'Menu' => 'Tlr\Menu\Laravel\MenuFacade'` to the `aliases` array.

You can then use the `Menu` facade as a shortcut for the `MenuRepository` class, like so:

```
$menu = Menu::menu( 'nav' );
$menu->item('Home')
```

after which, you can access it again later with `Menu::menu( 'nav' )`.

The Laravel version of the class can be echoed out (which will call the `render()` method). This renders the menu using Laravel's blade templating system. You have two options for customising:

- You can override the Menu Builder views (run `php artisan vendor:publish`, then edit those files).
- You can pass a view to the parent menu like so. This view will have the `MenuItem` object available as the `$menu` variable:

```
$menu->setView( 'my.menu.view' );
```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 95.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Recently: every ~0 days

Total

20

Last Release

4118d ago

Major Versions

v1.0.1 → v2.0.02014-02-18

2.2.x-dev → v3.0.02014-09-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/792e2c24d2cd9ca5c55787efcedc0035632fbd3f7df82fa2b6eaa7f2287e226c?d=identicon)[tedslittlerobot](/maintainers/tedslittlerobot)

---

Top Contributors

[![tedslittlerobot](https://avatars.githubusercontent.com/u/1783459?v=4)](https://github.com/tedslittlerobot "tedslittlerobot (71 commits)")[![stygiansabyss](https://avatars.githubusercontent.com/u/4187442?v=4)](https://github.com/stygiansabyss "stygiansabyss (2 commits)")[![pidgpowell](https://avatars.githubusercontent.com/u/1403420?v=4)](https://github.com/pidgpowell "pidgpowell (1 commits)")

---

Tags

laravellistmenu

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tlr-menu/health.svg)

```
[![Health](https://phpackages.com/badges/tlr-menu/health.svg)](https://phpackages.com/packages/tlr-menu)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[vinkius-labs/laravel-page-speed

Laravel Page Speed

2.5k12.5k1](/packages/vinkius-labs-laravel-page-speed)[emargareten/inertia-modal

Inertia Modal is a Laravel package that lets you implement backend-driven modal dialogs for Inertia apps.

90142.9k](/packages/emargareten-inertia-modal)[wearepixel/laravel-cart

A cart implementation for Laravel

1374.8k](/packages/wearepixel-laravel-cart)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
