Handling active menu item in Laravel 5

Date Posted: July 26, 2016, 12:44 am


Put this in your helper file:

1
2
3
4
5
6
function set_active( $route ) {
    if( is_array( $route ) ){
        return in_array(Request::path(), $route) ? 'active' : '';
    }
    return Request::path() == $route ? 'active' : '';
}

Usage:

1
2
3
<li class = "{{ set_active('admin/users') }}">
    <a href="{{ url('/admin/users/') }}">Users</a>
</li>

Multi-level:

1
2
3
4
5
6
7
8
9
10
11
12
13
<li class="{{ set_active(['admin/items', Request::is('admin/auction/*'), 'admin/something']) }}">
    <ul>
        <li class = "{{ set_active('admin/item') }}">
            <a href="{{ url('/admin/item') }}">All</a>
        </li>
        <li class = "{{ set_active('admin/item/create') }}">
            <a href="{{ URL::to('/admin/item/create') }}">Add New</a>
        </li>
        <li class = "{{ set_active('admin/something') }}">
            <a href="{{ URL::to('/admin/something') }}">Something</a>
        </li>
    </ul>
</li>

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me