Image Description
salahhusa9/laravel-menu
  • 15 Stars
  • 11 months ago

Laravel Menu Generator

Latest Version on Packagist GitHub Tests Action Status PHPStan Status Total Downloads

This is a useful package for building menus in your Laravel application, and it can help simplify the process of creating and managing menus in your application.

Installation

You can install the package via composer:

composer require salahhusa9/laravel-menu

You can publish the config file with:

php artisan vendor:publish --tag="menu-config"

Optionally, you can publish the views using

php artisan vendor:publish --tag="menu-views"

Usage

Basic

You can create menu in your application in any place you want, but the best place is in the AppServiceProvider.php file in the boot() method.

use SalahHusa9\Menu\Facades\Menu;

public function boot()
{
    Menu::add('test', 'route.name', 'fa fa-home');
}

Add items to the menu using the add() method. You can chain multiple add() calls to add multiple items.

Menu::add('test')
    ->add('test2');

To create a submenu, call addSubmenu() on a menu item and add items to the submenu using the add() method.

Menu::add('test')
    ->add('test2')
    ->addSubmenu('test3', function ($submenu) {
        $submenu->add('test4');
    })
    ->add('test5');

Blade

To render the menu, use the <x-menu /> blade component.

<x-menu />

Customization of the menu

Icons can be added to the menu items by passing the icon parameter to the add() method.

Menu::add('test', 'route.name', ['icon' => 'fa fa-home']);

You can also add a id and class to the menu item by passing the id and class parameters to the add() method.

Menu::add('test', 'route.name',['class' => 'customClass', 'id' => 'customId']);

You can also add a target to the menu item by passing the target parameter to the add() method.

Menu::add('test', 'route.name', ['target' => '_blank']);

You can also add a badge to the menu item by passing the badgeClass and badgeName parameters to the add() method.

Menu::add('test', 'route.name', ['badgeClass' => 'badge badge-success', 'badgeName' => 'New']);

You can also

// old:
if (auth()->user()->can('gateName')){
    Menu::add('test', 'route.name');
}
// new:
Menu::add('test', 'route.name', ['gateName' => 'gateName']);

Customization of the menu view

You can customize the menu view by publishing the views using

php artisan vendor:publish --tag="menu-views"

Multiple Menus

You can create multiple menus in your application:

    Menu::make('sidebar', function ($menu) {
        $menu->add('test', 'route.name');
    });

    Menu::make('main', function ($menu) {
        $menu->add('test', 'route.name');
    });

For render the menu, use the <x-menu for="sidebar" /> blade component.

Configuration of the defult menu Classes of Ul and Li and the active class

You can publish the config file with:

php artisan vendor:publish --tag="menu-config"

and you can change the defult menu Classes of Ul and Li and the active class

return [
    "ul_class" => "menu-inner py-1", // default menu class
    "ul_sub_menu_class" => "menu-sub", // default submenu class

    "li_class" => "menu-item", // default menu item class
    "li_sub_menu_class" => "menu-item", // default submenu item class
    "li_sub_menu_open_class" => "menu-item active open", // default submenu item class when open

    "a_class" => "menu-link", // default menu link class
    "a_sub_menu_class" => "menu-link menu-toggle", // default submenu link class

    "icon_class" => "menu-icon", // default menu icon class

    "li_active_class" => "active", // default active class of li
    "a_active_class" => "active", // default active class of a

    "badge_class" => "badge rounded-pill ms-auto", // default badge class
];

Advanced

Each Item accept this parames :

add(
    $name,
    $routeName = null,
    $options = [],
)

addSubmenu(
    $name,
    callback $callbackOfSubmenu,
    $options
)

There is other functions that you can used :

Menu::getMenuAsJson(); // return the menu as json
Menu::renderAsJson(); // return the menu as json
Menu::renderAsHtml(); // return the menu as html

Roadmap

See the open issues for a list of proposed features (and known issues).

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.