Introduction | menu | Spatie

 SPATIE

  HTML Menu Generator
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Menu](https://spatie.be/docs/menu/v3)  Introduction

 Version   v3   v2   v1

 Other versions for crawler [v3](https://spatie.be/docs/menu/v3) [v2](https://spatie.be/docs/menu/v2) [v1](https://spatie.be/docs/menu/v1)

- [ Introduction ](https://spatie.be/docs/menu/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/menu/v3/postcardware)
- [ Requirements ](https://spatie.be/docs/menu/v3/requirements)
- [ Installation and Setup ](https://spatie.be/docs/menu/v3/installation-and-setup)
- [ Questions &amp; Issues ](https://spatie.be/docs/menu/v3/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/menu/v3/changelog)
- [ About Us ](https://spatie.be/docs/menu/v3/about-us)

Basic usage
-----------

- [ Your First Menu ](https://spatie.be/docs/menu/v3/basic-usage/your-first-menu)
- [ Adding Items ](https://spatie.be/docs/menu/v3/basic-usage/adding-items)
- [ Building a menu from a data source ](https://spatie.be/docs/menu/v3/basic-usage/building-a-menu-from-a-data-source)

Items in depth
--------------

- [ Active Items ](https://spatie.be/docs/menu/v3/items-in-depth/active-items)
- [ Manipulating Items ](https://spatie.be/docs/menu/v3/items-in-depth/manipulating-items)
- [ Conditionally Adding Items ](https://spatie.be/docs/menu/v3/items-in-depth/conditionally-adding-items)

Controlling the html output
---------------------------

- [ Item Attributes ](https://spatie.be/docs/menu/v3/controlling-the-html-output/item-attributes)
- [ Parent Attributes ](https://spatie.be/docs/menu/v3/controlling-the-html-output/parent-attributes)
- [ Appending and Prepending Html ](https://spatie.be/docs/menu/v3/controlling-the-html-output/appending-and-prepending-html)
- [ Non-list Menus ](https://spatie.be/docs/menu/v3/controlling-the-html-output/non-list-menus)

Menus in your Laravel app
-------------------------

- [ Convenience Methods ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/convenience-methods)
- [ Views as Menu Items ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/views-as-menu-items)
- [ Conditional Items Based on Permissions ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/conditional-items-based-on-permissions)
- [ Using Macros ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/using-macros)

Examples
--------

- [ Bootstrap Menu ](https://spatie.be/docs/menu/v3/examples/bootstrap-menu)

 HTML Menu Generator
=====================

 Fluent interface to build HTML menus in PHP

 [    Repository ](https://github.com/spatie/menu)

    2,922,895

    759

Introduction
------------

###  On this page

1. [ Human Readable, Fluent Interface ](#content-human-readable-fluent-interface)
2. [ Strong Control Over the Html Output ](#content-strong-control-over-the-html-output)
3. [ Adding an ID to elements ](#content-adding-an-id-to-elements)
4. [ Not Afraid of Depths ](#content-not-afraid-of-depths)
5. [ Some Extra Treats for Laravel Apps ](#content-some-extra-treats-for-laravel-apps)
6. [ We have badges! ](#content-we-have-badges)

The `spatie/menu` package provides a fluent interface to build menus of any size in your php application. If you're building your app with Laravel, the `spatie/laravel-menu` provides some extra treats.

Human Readable, Fluent Interface
------------------------------------------------------------------------------------------------------------------------------------------------------

All classes provide a human readable, fluent interface (no array configuration). Additionally, you can opt for a more verbose and flexible syntax, or for convenience methods that cover most use cases.

```
Menu::new()
    ->add(Link::to('/', 'Home'))
    ->add(Link::to('/about', 'About'))
    ->add(Link::to('/contact', 'Contact'))
    ->render();

// Or just...
Menu::new()
    ->link('/', 'Home')
    ->link('/about', 'About')
    ->link('/contact', 'Contact');
```

```

    Home
    About
    Contact

```

Strong Control Over the Html Output
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

You can programatically add html classes and attributes to any item in the menu, or to the menu itself.

```
Menu::new()
    ->addClass('navigation')
    ->add(Link::to('/', 'Home')->addClass('home-link'))
    ->add(Link::to('/about', 'About'))
    ->add(Link::to('/contact', 'Contact')->addParentClass('float-right'))
    ->wrap('div', ['class' => 'wrapper'])
```

```

        Home
        About
        Contact

id('navigation')
    ->add(Link::to('/', 'Home')->id('home-link'))
    ->add(Link::to('/about', 'About'))
    ->add(Link::to('/contact', 'Contact'))
```

```

    Home
    About
    Contact

```

Not Afraid of Depths
--------------------------------------------------------------------------------------------------------------------

The menu supports submenus, which in turn can be nested infinitely.

```
Menu::new()
    ->add(Link::to('/', 'Home'))
    ->submenu('More', Menu::new()
        ->addClass('submenu')
        ->link('/about', 'About')
        ->link('/contact', 'Contact')
    );
```

```

    Home

        More

            About
            Contact

```

Some Extra Treats for Laravel Apps
--------------------------------------------------------------------------------------------------------------------------------------------------------------

The Laravel version of the menu package adds some extras like convenience methods for generating URLs and macros.

```
Menu::macro('main', function () {
    return Menu::new()
        ->action('HomeController@index', 'Home')
        ->action('AboutController@index', 'About')
        ->action('ContactController@index', 'Contact')
        ->setActiveFromRequest();
});
```

```

    {!! Menu::main() !!}

```

We have badges!
---------------------------------------------------------------------------------------------------

 [![spatie/menu](https://img.shields.io/badge/packagist-spatie/menu-orange.svg?style=flat-square)](https://packagist.org/packages/spatie/menu) [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/menu.svg?style=flat-square)](https://packagist.org/packages/spatie/menu) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/spatie/menu/blob/master/LICENSE.md) [![Build Status](https://img.shields.io/travis/spatie/menu/master.svg?style=flat-square)](https://travis-ci.org/spatie/menu) [![Quality Score](https://img.shields.io/scrutinizer/g/spatie/menu.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/menu) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/menu.svg?style=flat-square)](https://packagist.org/packages/spatie/menu) [![spatie/laravel-menu](https://img.shields.io/badge/packagist-spatie/laravel--menu-orange.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-menu) [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-menu.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-menu) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/spatie/laravel-menu/blob/master/LICENSE.md) [![Build Status](https://img.shields.io/travis/spatie/laravel-menu/master.svg?style=flat-square)](https://travis-ci.org/spatie/laravel-menu) [![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-menu.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-menu) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-menu.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-menu)
