PHP code example of elmdash / menu

1. Go to this page and download the library: Download elmdash/menu library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elmdash / menu example snippets


$m = new \ElmDash\Menu\Menu('top');

// a login route, only visible to guests
$m->add('access')->guests();

// menu items for users with proper permissions
$m->add('books.edit', function (Menu $books) {
	$books->can('edit-books');

	// It's optional to edit within callbacks.
	// However, this will be called once the menu is rendered
	// (instead of immediately)
});

// an adit route, active for any account routes
$m->add('account.edit')->match('account.*');

// you can also nest items
$c = $m->add('event.create');

// you may add route parameters that are 
// 



return [
  'top' => [
    'access'       => 'Sign up or sign in',
    'account-edit' => 'Settings',
    'event-create' => 'Create an event',
    'logout'       => 'Sign out',
  ],
];

$menu = new Menu('top-left');

$menu->add('my.route');

$menu->add('my.route')->add('my.child.route');

$menu->add('my.route', function (Menu $m) {
  // runs eventually, but not right away
  $m->add('my.child.route');
});

// this would trigger all callbacks and load the whole tree (likely called in your view)
$children = $menu->children();

// prepending
$menu->add('my.second.child');
$child = $menu->prepend('my.first.child');

// using groups (the name is _not_ a route name)
// they do not have a route associated with them. So you can't, for example, 
// attempt to get the href of a group. 
$child->group('some.group.name', function (Menu $g) {
	$g->add('my.grand.child');
});


// you can also prepend a group
$child->groupPrepend('some.other.group', function (Menu $g) {
	$g->add('my.step.grand.child');
});

$menu->add('my.first.route');

// or more manually
$child = $menu->add();
$child->route('my.route');

$menu->add('my.route.default', function (Menu $m) {

  // if the param is optional
  $m->params(['type' => null]); 
});

$menu->add('my.route.type', function (Menu $m) {
  $m->params(['type' => 'fish']);
});

$menu->add('my.route.type', function (Menu $m) {
  $m->params(['type' => 'mammals']);
});

foreach ($contexts as $ctx) {
    // list by context (i.e. /media/{context})
    $m->add('media.index')
        ->params(['context' => $ctx->handle])
        ->activeFor('media.*')
        
        // get the context from the media when editing that media 
        // (i.e. /media/item/{media}/edit)
        ->extractParams(function ($currentParams) {
            $media = array_get($currentParams, $media);
            if ($media) {
                return ['context' => $media->context->handle];
            }
            return [];
        });
}

// single glob
$users->activeFor('users.*');

// multi glob
$settings->activeFor('users.settings.*|account|account.edit.*');

// regex
$account->activeFor('/^users\.account\..*$/');

$left->add('wiki.index')->can('edit-wiki');

// during initialization
$m->flags(['ajax-only', 'bold']);

$menu->add('my.route.name'); // key is "my-route-name"

$menu = new Menu('top-left');
$menu->add('my.route.name'); // key is "top-left.my-route-name"

$menu = new Menu('top-left');
$menu->group('branding', function (Menu $g) {
	$g->add('my.route.name'); // key is "top-left.branding.my-route-name"
});

$menu = new Menu('top-left');
$menu->langNamespace('app');
$menu->add('my.route.name'); // key is "app::top-left.my-route-name"

$menu->add('my.route.name', function (Menu $m) {
  $m->labelKey('my-full-route-name');
});

$menu->add('my.route.name', function (Menu $m) {
  $m->label('My Route Here!');
});