Effortless menu building for Laravel 4

1.4.1 2018-10-15 09:52 UTC

This package is auto-updated.

Last update: 2024-10-16 02:51:30 UTC


README

http://luke.sno.wden.co.uk/menu for more information and for feature requests.

Usage

Very simple method of building menus from database data (id, parent id), service provider register methods for application modules and much more.

Standalone

use LukeSnowden\Menu\Menu;
include __DIR__ . '/vendor/autoload.php';

Example 1

<?php
Menu::addItem( array( 'text' => 'Home', 'URL' => '/', 'reference' => '0' ) );
echo Menu::render();
?>

##Example 2 - Nesting Children

<?php
Menu::addItem( array( 'text' => 'Services', 'URL' => '/services/', 'reference' => '1', 'parent' => '0' ) );
echo Menu::render();
?>

Example 3 - Multiple Menus

<?php
Menu::addItem( array( 'text' => 'Services', 'URL' => '/services/', 'reference' => '1', 'parent' => '0' ) )->toMenu( 'main' );
echo Menu::render( 'main' );
?>

Auto classes

I have added in some of the most used and required classes for styling menus

.first-item {}
.last-item {}
.current-root {}
.current-parent {}
.current-ancestor {}
.has-children {}

Output

<?php
Menu::addItem( array( 'text' => 'Home', 'URL' => '/menu-test-2/public/', 'reference' => '1', 'class' => 'home-icon', 'weight' => 0 ) )->toMenu( 'main' );
Menu::addItem( array( 'text' => 'Services', 'URL' => '/menu-test-2/public/services/', 'reference' => '2' ) )->toMenu( 'main' );
Menu::addItem( array( 'text' => 'Development', 'URL' => '/menu-test-2/public/services/development/', 'reference' => '3', 'parent' => '2' ) )->toMenu( 'main' );
Menu::addItem( array( 'text' => 'Design', 'URL' => '/menu-test-2/public/services/design/', 'reference' => '4', 'parent' => '2', 'weight' => 0 ) )->toMenu( 'main' );
echo Menu::render( 'main' );
?>
<ul class="nav-main">
    <li class="home-icon current first-item container node-1">
        <a href="/menu-test-2/public/">Home</a>
    </li>
    <li class=" has-children last-item container node-1">
        <a href="/menu-test-2/public/services/">Services</a>
        <ul>
            <li class=" first-item nav-node node-2">
                <a href="/menu-test-2/public/services/design/">Design</a>
            </li>
            <li class=" last-item nav-node node-2">
                <a href="/menu-test-2/public/services/development/">Development</a>
            </li>
        </ul>
    </li>
</ul>

Custom attributes

$name = false, $attributes = array(), $htmlTag = 'ul'

echo Menu::render( 'main', array( 'class' => 'nav nav-pills nav-stacked', 'role' => 'tablist' ), 'nav' );

Custom Layout Render

You may want to change the output layout (demo render class included)

Menu::addItem( array( 'text' => 'Home', 'URL' => '/menu-test-2/public/', 'reference' => '1', 'class' => 'home-icon', 'weight' => 0 ) )->toMenu( 'main' );
Menu::addItem( array( 'text' => 'Services', 'URL' => '/menu-test-2/public/services/', 'reference' => '2' ) )->toMenu( 'main' );
Menu::addItem( array( 'text' => 'Development', 'URL' => '/menu-test-2/public/services/development/', 'reference' => '3', 'parent' => '2' ) )->toMenu( 'main' );
Menu::addItem( array( 'text' => 'Design', 'URL' => '/menu-test-2/public/services/design/', 'reference' => '4', 'parent' => '2', 'weight' => 0 ) )->toMenu( 'main' );

Menu::setMenuType( 'horizontal', 'main', 'LukeSnowden\Menu\Styles' );
echo Menu::render( 'main' );

Use with third party menu UI through L4 Model

(Please note this is just a general summary of how it would work if you had 2 tables (and models) for navigations and navigation items with a standard hasMany() relationship)

<?php
$navigation = Navigation::with( 'navigationItems' )->where( 'navigation_slug', '=', 'main' )->get();
foreach( $navigation->navigationItems as $item )
{
    Menu::addItem( array( 'text' => , $item->name 'URL' => $item->url, 'reference' => $item->id, 'parent' => $item->parent_id, 'weight' => $item->order ) )->toMenu( $navigation->navigation_slug );
}
echo Menu::render( $navigation->navigation_slug );
?>

Laravel Install

Add the following to you applications composer.json file

composer require lukesnowden/menu:dev-master

add the following to your /app/config/app.php's provider array.

'LukeSnowden\Menu\MenuServiceProvider'

add the following to your /app/config/app.php's aliases array.

'Menu'      => 'LukeSnowden\Menu\Facades\Menu'

and finally back to your terminal and run

php composer.phar dump-autoload