Introduction | menu | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Menu](https://spatie.be/docs/menu/v1)  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/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/menu/v1/postcardware)
- [ Requirements ](https://spatie.be/docs/menu/v1/requirements)
- [ Installation and Setup ](https://spatie.be/docs/menu/v1/installation-and-setup)
- [ Questions &amp; Issues ](https://spatie.be/docs/menu/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/menu/v1/changelog)
- [ About Us ](https://spatie.be/docs/menu/v1/about-us)

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

- [ Your First Menu ](https://spatie.be/docs/menu/v1/basic-usage/your-first-menu)
- [ Adding Items ](https://spatie.be/docs/menu/v1/basic-usage/adding-items)

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

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

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

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

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

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

Examples
--------

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

API
---

- [ Menu ](https://spatie.be/docs/menu/v1/api/menu)
- [ Link ](https://spatie.be/docs/menu/v1/api/link)
- [ Html ](https://spatie.be/docs/menu/v1/api/html)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/menu                                                                                                                                                                                                                                    `

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

 Fluent interface to build HTML menus in PHP

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

    2,868,359

    758

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. [ Not Afraid of Depths ](#content-not-afraid-of-depths)
4. [ Some Extra Treats for Laravel Apps ](#content-some-extra-treats-for-laravel-apps)
5. [ 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.wrapper')
```

```

        Home
        About
        Contact

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) [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/3f243a38-a1c7-42f5-96c8-37526e807029.svg?style=flat-square)](https://insight.sensiolabs.com/projects/3f243a38-a1c7-42f5-96c8-37526e807029) [![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) [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/3f243a38-a1c7-42f5-96c8-37526e807029.svg?style=flat-square)](https://insight.sensiolabs.com/projects/3f243a38-a1c7-42f5-96c8-37526e807029) [![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)
