Active States based on Route names

Submitted by chadwithuhc - 9 years ago

There are a few tips already for active states on navigation, however none are based on Route names. I prefer to use Route names for all routes, so here is a modified version of the "active states" trick for route names.

 * Tests a route against the current URL for active state
 * If true, returns 'selected' for class name
 * Usage: HTML::activeState('named.route')
HTML::macro('activeState', function ($route) {
    return strpos(Request::url(), route($route)) !== false ? 'selected' : '';

 * - Code can be placed just about anywhere. I would recommend including a common
 *     file for macros.
 * - Usage will match any resource route within a section if you pass in the base route.
 *     So `dashboard.reports.index` == 'dashboard/reports', then if you reside at
 *     `dashboard.reports.create` and try `HTML::activeState('dashboard.reports.index')`
 *     you will still get the active class since the root 'dashboard/resports' exists in the path.