PHP code example of red-freak / laravel-menu

1. Go to this page and download the library: Download red-freak/laravel-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/ */

    

red-freak / laravel-menu example snippets


Menu::add('home')
    ->add(new Item('Home', '/'))
    ->add(new Item('About', '/about'))
    ->add(new Item('Login', '/login'));

// define the menu anywhere to use translation keys as labels (or do it by config)
Menu::add('home', [
    RenderOptions::KEY_USE_LABELS_AS_TRANSLATION_KEYS => true
]);

...

// have the corresponding translation keys via files (or in this case via the `Translator`-Facade)
app()->translator->addLines([
    'menu.label.home' => 'Home',
    'menu.label.about' => 'About',
    'menu.label.login' => 'Login',
], 'en');

...

// Now you can add items via the `Item` class ...
Menu::get('home')
    ->add(new Item('menu.label.home', 'http://localhost'))
    ->add(new Item('menu.label.about', 'http://localhost/about'))
    ->add(new Item('menu.label.login', 'http://localhost/login'));

// ... or add them via key-value ...
Menu::get('home')
    ->add([
        Menu::KEY_ITEM_LABEL => 'Home',
        Menu::KEY_ITEM_LINK => 'http://localhost',
    ])
    ->add([
        Menu::KEY_ITEM_LABEL => 'About',
        Menu::KEY_ITEM_LINK => 'http://localhost/about',
    ])
    ->add([
        Menu::KEY_ITEM_LABEL => 'Login',
        Menu::KEY_ITEM_LINK => 'http://localhost/login',
    ]);

// ... or add them via routes ...
// Route::get('/', fn() => 'home')->name('home');
// Route::get('/about', fn() => 'about')->name('about');
// Route::get('/login', fn() => 'login')->name('login');
Menu::get('home')
    ->add([Menu::KEY_ITEM_ROUTE => 'home'])
    ->add([Menu::KEY_ITEM_ROUTE => 'about'])
    ->add([Menu::KEY_ITEM_ROUTE => 'login']);

// define the route and the translation keys
Route::resource('users', Controller::class);
app()->translator->addLines([
    'menu.label.users.index' => 'Users',
    'menu.label.users.create' => 'create User',
], 'en');

// define the menu and add a submenu by a model
Menu::add('home', [RenderOptions::KEY_USE_LABELS_AS_TRANSLATION_KEYS => true])->add([
    Menu::KEY_ITEM_MODEL => User::class,
]);