PHP code example of javaabu / menu-builder

1. Go to this page and download the library: Download javaabu/menu-builder 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/ */

    

javaabu / menu-builder example snippets




namespace App\Menus;

use Javaabu\MenuBuilder\Menu\Menu;
use Javaabu\MenuBuilder\Menu\MenuItem;

class AdminSidebar extends Menu implements IsMenu
{
    protected string $id = 'side-bar';
    protected string $icon_prefix = 'zmdi zmdi-';
    protected ?string $guard = 'web_admin';

    public function menuItems(): array
    {
        return [
            MenuItem::make(__('Dashboard'))                 
                    ->route('admin.home')
                    ->icon('zmdi-view-quilt')
                    ->cssClass('custom-css-class'),
                    
            MenuItem::make(__('Users'))
                ->controller(\App\Http\Controllers\Admin\UsersController::class)
                ->can('viewAny', \App\Models\User::class)
                ->icon('zmdi-accounts')
                ->count(App\Models\User::userVisible()->pending(), 'approve_users'),

            MenuItem::make(__('Roles'))
                ->url('/roles?foo=bars')
                ->permissions('view_roles')
                ->active(function (Request $request) {
                    return $request->query('foo') == 'bars';
                })
                ->badge(__('New'), 'text-bg-primary'),
        ];
    }
}

$sidebar = new \App\Menus\AdminSidebar();

{!! $sidebar->links() !!}

// render as Bootstrap 5
{!! $sidebar->useBootstrap5()->links() !!}

// render as Material Admin 2.6
{!! $sidebar->useMaterialAdmin26()->links() !!}

use Javaabu\MenuBuilder\MenuBuilder;
 
/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    MenuBuilder::useBootstrap5();
    MenuBuilder::useMaterialAdmin26();
}

// render using custom view
{!! $sidebar->links('web.menus.sidebar') !!}

$item->getLink() // returns the link of the item
$item->getLabel() // returns the label of the item
$item->isActive() // checks if the item is currently active
$item->hasActiveChild($user) // checks if the item has any visible child that is currently active
$item->hasIcon() // checks if the item has an icon defined
$item->getIcon($icon_prefix) // returns the item's icon with the prefix prepended
$item->hasCssClass() // checks if the items has a custom css class set
$item->getCssClass() // returns the custom css class, if any set for the item
$item->getAggregatedCount($user) // get the count of the item + the count of all visible child items, will return 0 if the current user can't see the count
$item->getVisibleCount($user) // get the count of the item, will return 0 if the current user can't see the count
$item->getVisibleChildren($user) // returns an array of all visible child items
bash
php artisan vendor:publish --provider="Javaabu\MenuBuilder\MenuBuilderServiceProvider" --tag="menu-builder-views"