roboc/menu-builder

Menu builder

Installs: 130

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:project

dev-master 2016-08-03 14:28 UTC

This package is auto-updated.

Last update: 2024-10-27 10:27:41 UTC


README

Easy way to build navigation menus

Scrutinizer Code Quality Build Status

Example usage

Create Menu

$menu = new Menu('main-menu');

Add menu items

$menu->item('Home')->to('/');

Use badges

$menu->item('Inbox')->to('inbox')->badge(5);

Create submenu

$menu->item('Reports')
    ->subMenu( function ( Menu $menu )
    {
        $menu->item('Summary')->to('/reports/summary');
        $menu->item('Detailed')->to('/reports/detailed');
    } );

Render menu

Using JSON renderer

$json = ( new \Roboc\Menu\Renderer\JsonRenderer )->render( $menu );

Output:

[
    {
        "title": "Home",
        "link": "\/"
    },
    {
        "title": "Inbox",
        "link": "inbox",
        "badge": {
            "value": 5,
            "attributes": []
        }
    },
    {
        "title": "Reports",
        "children": [
            {
                "title": "Summary",
                "link": "\/reports\/summary"
            },
            {
                "title": "Detailed",
                "link": "\/reports\/detailed"
            }
        ]
    }
]

Using simple HTML renderer

$html = ( new \Roboc\Menu\Renderer\HtmlRenderer )->render( $menu );

Output:

<ul id="main-menu">
    <li><a href="/">Home</a></li>
    <li><a href="/inbox">Inbox <span class="badge">5</span></a></li>
    <li>Reports
        <ul>
            <li><a href="/reports/summary">Summary</a></li>
            <li><a href="/reports/detailed">Detailed</a></li>
        </ul>
    </li>
</ul>

Other

You can create your own menu renderer or build menu output direclty in your code