Menu active with parameters and attributes supported!

Submitted by marceloferro - 9 years ago

Hi, I got the example by Dastan Turysbekov (http://laravelsnippets.com/snippets/menu-activated) and changed a little bit. It's a simple Macro to use with Active Menu. You can use parameters and attributes as well.

//view.blade.php
 
<div class="menu">
    <ul>
        {{ HTML::menu_active('home', 'Home') }}
        {{ HTML::menu_active('account', 'Account') }}
        {{ HTML::menu_active('user-edit', 'Edit user', ['id' => Auth::id()]) }}
    </ul>
</div>

//macro.php
 
HTML::macro('menu_active', function($route, $title, array $parameters = array(), array $attributes = array())
{
    $url = str_replace(Request::root() . '/', '', route($route));
 
    if ( Request::is( $url ) || Request::is( $url . '/*') ) {
        $active ='<li class="active">' . link_to_route($route, $title, $parameters, $attributes) . '</li>';
    }
    else {
        $active ='<li>' . link_to_route($route, $title, $parameters, $attributes) . '</li>';
    }
 
    return $active;
});