jason
4842
39
General

Best practice for handling "active" menu item in L5

Posted 4 years ago by jason

I've been trying to wrap my head around how best to address marking a menu item as the current page (I'm using twitter bootstrap for front-end css). In L4 it is suggested to do something like this:

<li class="{{ Request::is('home') ? 'class="active"' : '' }}"><a href="{{URL::to('clients')}}">Home</a></li>

This doesn't seem to work in L5 though.

In attempt to stay DRY, I implemented a Nav helper class with the following method:

    public static function setActive($path, $request, $active = 'active')
    {
        return $request->is($path) ? $active : '';
    }

in my blade template I can do this:

<li class="{{\Webapp\Helpers\Nav::setActive('home', $request)}}">

but I will have to return the request in my controllers for every route that I want to enable for active menu navigation:

return view('home', compact('request'));

I swear I saw a laracast that touched on this subject but am having trouble finding it again.

Is returning the request in every controller method the best way to handle active menu items? Is there a more elegant solution?

EDIT: found the laracast I was looking for but would still like to get feedback from others on solutions that work well in L5:

https://laracasts.com/lessons/active-states

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.