Laravel 5 Set Active Navigation State

Example of how you can set the active state on a navigation item which matches the page that you are viewing. Pretty basic stuff but a necessity for most websites.

In this example I’ve added a static helper function to my helpers class and auto loaded that class in composer.json. I then call that function from the nav blade template and bobs your uncle.

helper function:

In nav template:

Then when users page is being viewed it appears active.


Extra credit.

You might want the nav to still be active for “Users” when we are viewing a users profile (ie  – /users/profile). This is how you might go about doing this dynamically.

Sam Deering is a web developer from England (currently living in Australia). In his spare time he enjoys coding, playing chess, reading and investing.

One thought on “Laravel 5 Set Active Navigation State

Leave a Reply

Your email address will not be published. Required fields are marked *