PHP code example of radiatecode / laravel-navbar

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

    

radiatecode / laravel-navbar example snippets


$navitems = Nav::make()
    ->add('Home', route('home'), ['icon' => 'fa fa-home'])
    ->header('Adminland', function (Nav $nav) {
        $nav
            ->add('Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
            ->add('Users', route('system-user-list'), ['icon' => 'fa fa-users']);
    })
    ->header('Employee Management', function (Nav $nav) {
        $nav
            ->add('Employee', '#', ['icon' => 'fa fa-user'], function (Children $children) {
                $children
                    ->add('List', route('employee-list'), ['icon' => 'fa fa-list'])
                    ->add('Create', route('create-employee'), ['icon' => 'fa fa-plus-circle']);
            })
            ->add('Transfer', '#', ['icon' => 'fa fa-money-check-alt'], function (Children $children) {
                $children
                    ->add('List', route('transfer-list'), ['icon' => 'fa fa-list'])
                    ->add('Create', route('create-transfer'), ['icon' => 'fa fa-plus-circle']);
            });
    })
    ->render(); // array of nav items

$navbar = Navbar::navs($navitems)->render(); // navbar html

use RadiateCode\LaravelNavbar\Nav;
use RadiateCode\LaravelNavbar\Children;
use RadiateCode\LaravelNavbar\Facades\Navbar;

class ViewServiceProvider extends ServiceProvider
{

    public function boot()
    {
        View::composer('layouts.partials._left_nav',function(View $view){
            $navitems = Nav::make()
                ->addIf(condition: true, 'Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
                ->add('Users', route('system-user-list'), ['icon' => 'fa fa-users'])
                ->add('Employee', '#', ['icon' => 'fa fa-user'], function (Children $children) {
                    $children
                        ->addif(condition: true, 'List', route('employee-list'), ['icon' => 'fa fa-list'])
                        ->addif(condition: false, 'Create', route('create-employee'), ['icon' => 'fa fa-plus-circle']);
                })
                ->render(); // array of nav items

                // Navbar UI builder
                $navbar = Navbar::navs($navitems)); 

                // Now attach the $navbar to your view.
                $view->with('navbar', $navbar->render();
        });

        // Or you can use `class based view composer`. place the Navbar generator code inside the compose().
        View::composer('layouts.partials._left_nav', NavComposer::class);
    }

}

// example
Nav::make()
->header('Adminland', function (Nav $nav) {
    // add nav items under the Adminland header
})

//Example 1
$navitems = Nav::make()
            ->add('Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
            ->add('Users', route('user-list'), ['icon' => 'fa fa-users'])
            ->render();

// Example 2: with header
$navitems = Nav::make()
        ->header('Adminland', function (Nav $nav) {
            $nav
                ->add('Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
                ->add('Users', route('system-user-list'), ['icon' => 'fa fa-users'])
                ->add('Settings', route('system-settings'), ['icon' => 'fa fa-wrench'])
        })
        ->render();
 
//Example 1
$navitems = Nav::make()
        ->addIf(true, 'Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
        ->addIf(false, 'Users', route('user-list'), ['icon' => 'fa fa-users'])
        ->render();

//Example 2: with header
        $navitems = Nav::make()
        ->header('Adminland', function (Nav $nav) {
            $nav
                ->addIf(true, 'Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
                ->addIf(false, 'Users', route('system-user-list'), ['icon' => 'fa fa-users'])
                ->addIf(true, 'Settings', route('system-settings'), ['icon' => 'fa fa-wrench'])
        })
        ->render();

// Example
$navitems = Nav::make()
->header('Employee Management', function (Nav $nav) {
    $nav
        ->add('Employee', '#', ['icon' => 'fa fa-user'], function (Children $children) {
            $children
                ->add('List', route('employee-list'), ['icon' => 'fa fa-list'])
                ->add('Create', route('create-employee'), ['icon' => 'fa fa-plus-circle']);
        })
        ->add('Transfer', '#', ['icon' => 'fa fa-money-check-alt'], function (Children $children) {
            // we can also conditionally add children nav
            $children
                ->addIf(true, 'List', route('transfer-list'), ['icon' => 'fa fa-list'])
                ->addIf(true, 'Create', route('create-transfer'), ['icon' => 'fa fa-plus-circle']);
        })
})
->render();

// render() result sample
[
    "home" => [
        "title" => "Home",
        "url" => "http://hrp.test/",
        "attributes" => [
            "icon" => 'fa fa-home'
        ],
        "is_active" => false,
        "type" => "menu",
        "children" => [] // no children
    ],
    "adminland" => [ // header
        "title" => "Adminland",
        "attributes" => [],
        "type" => "header",
        "nav-items" => [ // nav items under the adminland header
            'roles' => [
                "title" => "Roles",
                "url" => "http://hrp.test/role-list",
                "attributes" => [
                    "icon" => 'fa fa-user-tag'
                ],
                "is_active" => false,
                "type" => "menu",
                "children" => [] // no children
            ],
            'user' => [
                "title" => "User",
                "url" => "http://hrp.test/system-user-list",
                "attributes" => [
                    "icon" => 'fa fa-users'
                ],
                "is_active" => false,
                "type" => "menu",
                "children" => [] // no children
            ]
        ]
    ],
    "employee-management"  => [ // header
        "title" => "Employee Management",
        "attributes" => [],
        "type" => "header", 
        "nav-items" => [ // nav items under the employee managment
            'employee' => [
                "title" => "Employee", // parent nav
                "url" => "#",
                "attributes" => [
                    "icon" => 'fa fa-user'
                ],
                "is_active" => false,
                "type" => "menu",
                "children" => [ // children nav items of employee nav
                    'list' => [
                        "title" => "List",
                        "url" => "http://hrp.test/employee-list",
                        "attributes" => [
                            "icon" => 'fa fa-list'
                        ],
                        "is_active" => false,
                        "type" => "menu",
                        "children" => []
                    ],
                    'create' => [
                        "title" => "Create",
                        "url" => "http://hrp.test/create-employee",
                        "attributes" => [
                            "icon" => 'fa fa-plus-circle'
                        ],
                        "is_active" => false,
                        "type" => "menu",
                        "children" => []
                    ]
                ]
            ]
        ]
    ],
]

    // Example of nav active script
    $navbar = Navbar::navs($navitems); 

    $view->with('navbar', $navbar->render())
         ->with('navScript',$navbar->navActiveScript());
    

 /**
 * Presenter for navbar style
 * 
 * [HTML presenter]
 */
'nav-presenter' => NavbarPresenter::class,

/**
 * It will set active to requested/current nav
 * 
 * [Note: if you want to set nav active by front-end (Js/Jquery) 
 * Or, if you cached your rendered navbar, then you should disable it]
 */
'enable-nav-active' => true
html
    <!-- assume you have layouts.partials._left_nav.blade.php -->

    <div class="sidebar">
        <!-- Sidebar Menu -->
        {!! $navbar !!}
        <!-- /.sidebar-menu -->
    </div>

    <!-- Note: We assume you have @stack('js') in your template layout-->
    @prepend('js')
        {!! $navScript !!}
    @endprepend
    <!-- ./ end Js-->
    
html
    <!-- assume: you have layouts.partials._script.blade.php -->

    {!! RadiateCode\LaravelNavbar\Facades\Navbar::navActiveScript(); !!}

    <script>
        // other js code
    </script>