Instantly share code, notes, and snippets.
Last active
April 2, 2019 08:43
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save ManojKiranA/56a9fd3332795edec8cc80b4dd4928d3 to your computer and use it in GitHub Desktop.
Setting the Active Menu in laravel (i have moved all the functions to my laravel package you can download it form here https://github.com/ManojKiranA/Aktiv)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Route; | |
class ActiveMenus | |
{ | |
/** | |
* @function isRouteActive | |
* @author Manojkiran | |
* @contact manojkiran10031998@gmail.com | |
* @param string $routeName | |
* @param string $className | |
* @usage Detect The Give route and return as Active | |
* @version 1.3 | |
**/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Detect Active Route | |
|-------------------------------------------------------------------------- | |
| | |
| Compare given route with current route and return className if they matches. | |
| Very useful for navigation, marking if the link is active. | |
| | |
|Usage: | |
|Option 1: Returning the bootstrap active class | |
|<li class="{{ ActiveMenu::isRouteActive('routeName') }}"> | |
| | |
|Option 2: Returning the Custom active class | |
|<li class="{{ ActiveMenu::isRouteActive('routeName','activeClassName') }}"> | |
| | |
*/ | |
public static function isRouteActive($routeName='',$className='') | |
{ | |
if ($className == null) | |
{ | |
$className = "active"; | |
} | |
if (Route::currentRouteName() == $routeName) | |
{ | |
return $className; | |
} | |
} | |
/** | |
* @function areRoutesActive | |
* @author Manojkiran | |
* @contact manojkiran10031998@gmail.com | |
* @param array $routeNames | |
* @param string $className | |
* @usage Detect The Give routes and return as Active | |
* @version 1.3 | |
**/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Detect Active Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Compare given array of routes with current route and return className if they matches. | |
| Very useful for navigation, marking if the link is active. | |
| | |
|Usage | |
|Option 1: Returning the bootstrap active class | |
|<li class="{{ ActiveMenu::areRoutesActive(['routeName1','routeName2','routeName3',routeNameN]) }}"> | |
| | |
|Option 2: Returning the Custom active class | |
|<li class="{{ ActiveMenu::areRoutesActive(['routeName1','routeName2','routeName3',routeNameN],'activeClassName') }}"> | |
| | |
*/ | |
public static function areRoutesActive(Array $routeNames,$className='') | |
{ | |
if ($className == null) | |
{ | |
$className = "active"; | |
} | |
foreach ($routeNames as $routeName) | |
{ | |
if (Route::currentRouteName() == $routeName) | |
{ | |
return $className; | |
} | |
} | |
} | |
/** | |
* @function isResourceActive | |
* @author Manojkiran | |
* @contact manojkiran10031998@gmail.com | |
* @param string $resourceName | |
* @param string $className | |
* @usage Detect The Give resources and return as Active | |
* @version 1.5 | |
**/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Detect Active Resource | |
|-------------------------------------------------------------------------- | |
| | |
| Compare given array of resource with current resource and return className if they matches. | |
| Very useful for navigation, marking if the link is active. | |
| | |
|Usage: | |
|Option 1: Returning the bootstrap active class | |
|<li class="{{ ActiveMenu::isResourceActive('resourceName') }}"> | |
| | |
|Option 2: Returning the Custom active class | |
|<li class="{{ ActiveMenu::isResourceActive('resourceName','activeClassName') }}"> | |
| | |
*/ | |
public static function isResourceActive($resourceName,$className='') | |
{ | |
if ($className == null) | |
{ | |
$className = "active"; | |
} | |
$routeResourceLists = ['.index','.create','.edit','show']; | |
foreach ($routeResourceLists as $routeResourceList) | |
{ | |
$activateEachRoute = $resourceName . $routeResourceList; | |
if (Route::is($activateEachRoute)) | |
{ | |
return $className; | |
} | |
} | |
} | |
/** | |
* @function areResourcesActive | |
* @author Manojkiran | |
* @contact manojkiran10031998@gmail.com | |
* @param array $resourceNames | |
* @param string $className | |
* @usage Detect The Give resources and return as Active | |
* @version 1.5 | |
**/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Detect Active Resources | |
|-------------------------------------------------------------------------- | |
| | |
| Compare given array of resources with current resources and return className if they matches. | |
| Very useful for navigation, marking if the link is active. | |
| | |
|Usage | |
|Option 1: Returning the bootstrap active class | |
|<li class="{{ ActiveMenu::areResourcesActive(['resourceName1','resourceName2','resourceName3',resourceNameN]) }}"> | |
| | |
|Option 2: Returning the Custom active class | |
|<li class="{{ |ActiveMenu::areResourcesActive(['resourceName1','resourceName2','resourceName3',resourceNameN],'activeClassName') }}"> | |
| | |
*/ | |
public static function areResourcesActive(Array $resourceNames,$className='') | |
{ | |
if ($className == null) | |
{ | |
$className = "active"; | |
} | |
foreach($resourceNames as $resourceName) | |
{ | |
$routeResourceLists = ['.index','.create','.edit','show']; | |
foreach ($routeResourceLists as $routeResourceList) | |
{ | |
$activateEachRoute = $resourceName . $routeResourceList; | |
if (Route::is($activateEachRoute)) | |
{ | |
return $className; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment