Laravel Add active class to Nav Menu System

Today, We want to share with you Laravel Add active class to Nav Menu System.In this post we will show you Dynamically Add Active Classes To Nav With PHP and Bootstrap, hear for Laravel dynamically add active classes to nav with php and bootstrap we will give you demo and example for implement.In this post, we will learn about Add active class to nav links based on URL with Laravel Address with an example.

Laravel Add active class to Nav Menu System

There are the Following The simple About Laravel Add active class to Nav Menu System Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel Add Active Navigation Class Based on URL, so the Laravel Dynamic Menu System for this example is following below.

Also Read :  Get Radio Button value using AngularJS

Setp 1: Laravel Blade File HTML

sidebar.blade.php

<h2>Laravel Add active class to Nav Menu System</h2>
<!-- Sidebar Menu -->
<aside class="main-sidebar">
    <section class="sidebar">
        <ul id="sidebarnav">
                <li {!! classSelectedPath('home') !!}><a href="{{ url('home') }}" {!! classActivePath('home') !!}><i class="fa fa-tachometer"></i><span>Dashboard</span></a></li>
                <li {!! classSelectedPath('members') !!}><a href="{{ url('members') }}" {!! classActivePath('members') !!}><i class="fa fa-ticket"></i><span>All Members</span> <span class="badge badge-pill badge-warning
">{{ $activemembers }}</span></a> </li>
                <li {!! classSelectedPath('complete') !!}><a href="{{ url('complete') }}" {!! classActivePath('complete') !!}><i class="fa fa-folder"></i><span>Completed Members</span> <span class="badge badge-pill badge-warning
">{{$completemembers}}</span></a> </li>
                    <li {!! classSelectedPath('Stores') !!}><a href="{{ url('Stores') }}" {!! classActivePath('Stores') !!}><i class="fa fa-shopping-bag"></i><span>Stores</span></a></li>
                    <li {!! classSelectedPath('Repots') !!}><a href="{{ url('Repots') }}"  classActivePath('Repots') !!}><i class="fa fa-list-ol"></i><span>Repots</span></a></li>
                    <li {!! classSelectedPath('Balance') !!}><a href="{{ url('Balance') }}" classActivePath('Balance') !!}><i class="fa fa-list-ul"></i><span>Balance</span></a></li>
                    <li {!! classSelectedPath('earnings') !!}><a href="{{ url('earnings') }}" {!! classActivePath('earnings') !!}><i class="fa fa-sliders"></i><span>Earnings</span></a></li>
                    <li {!! classSelectedPath('Menus') !!}><a href="{{ url('Menus') }}" {!! classActivePath('Menus') !!}><i class="fa fa-sitemap"></i><span>Menus</span></a></li>
                    <li {!! classSelectedPath('modes') !!}><a href="{{ url('modes') }}" {!! classActivePath('modes') !!}><i class="fa fa-tasks"></i><span>Modes</span></a></li>
                    <li {!! classSelectedPath('Auto-response') !!}><a href="{{ url('Auto-response') }}" {!! classActivePath('Auto-response') !!}><i class="fa fa-reply-all"></i><span>Auto response</span></a></li>
                    <li {!! classSelectedPath('ticket-email-card') !!}><a href="{{ url('ticket-email-card') }}" {!! classActivePath('ticket-email-card') !!}><i class="fa fa-envelope"></i><span>Email Card</span></a></li>
                    <li {!! classSelectedPath('permissions') !!}><a href="{{ url('permissions') }}" {!! classActivePath('permissions') !!}><i class="fa fa-users"></i><span>Permissions</span></a></li>
                    <li {!! classSelectedPath('settings') !!}><a href="{{ url('settings') }}" {!! classActivePath('settings') !!}><i class="fa fa-wrench"></i><span>Settings</span></a></li>
        </ul>
    </section>
    <!--<div class="plan-icon">
        <a href="plan.html" class="plan-btn">
            <img src="images/plan.svg" alt="">
        </a>
    </div>-->
</aside>
<!-- End Sidebar Menu -->
<a href="https://pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a>

Setp 2: Create Custom Laravel helpers

app/library/helpers.php

<?php
if (!function_exists('classActivePath')) {
    function classActivePath($path)
    {
        $parts = explode('/', \Request::path());
        if ($parts[0] == $path || \Request::is($path)) {
            return 'class="active"';
        }

        return '';
    }
}
if (!function_exists('classSelectedPath')) {
    function classSelectedPath($path)
    {
        $parts = explode('/', \Request::path());
        if ($parts[0] == $path || \Request::is($path)) {
            return 'class="selected"';
        }

        return '';
    }
}

Setp 3: project_name/composer.json

Add Register Path : “files”:[“app/library/helpers.php”]

Also Read :  Laravel 5.6 Server Side Validation Example

Include globally in composer.json

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files":["app/library/helpers.php"]
},
"autoload-dev": {
    "psr-4": {
        "Tests\": "tests/"
    }
},
"extra": {
    "laravel": {
        "dont-discover": [
        ]
    }
},

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Add active class to Nav Menu System.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Gondaliya Jaydeep

Hi, I’m Jaydeep Gondaliya, PHP developer.Pakainfo.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]