Created
May 3, 2015 13:35
-
-
Save newtonsbm/20445cd9cd3cf0cc49ee to your computer and use it in GitHub Desktop.
CakePHP Helper to build a bootstrap menu item with an font-awesome icon
This file contains hidden or 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 | |
App::uses('AppHelper', 'View/Helper'); | |
class BootstrapHelper extends AppHelper { | |
public $helpers = array('Html'); | |
/** | |
* navLi: adds a li item with a link | |
* | |
* @param string $title Text for link | |
* @param string $icon_class font-awesome fa-* icon class | |
* @param string $controller controllers name | |
* @param string $action action | |
* @param string $pages name of the static page in case of pages controller | |
* @param array $prefixes array containing the prefixes in case a prefix route | |
* @param string $plugin name of a plugin to route | |
* @return li element with a link to be used ina bootstrap menu | |
*/ | |
public function navLi($title, $icon_class, $controller, $action, $pages, $prefixes=null, $plugin=null) { | |
$title_icon = "<i class='fa fa-fw $icon_class'></i> $title"; | |
$class = ($this->params['controller'] == $controller && $this->params['action'] == $action) ? 'active' : ''; | |
$routingPrefixes = Configure::read('Routing.prefixes'); | |
$url = array('controller' => $controller, 'action' => $action, $pages); | |
$url['plugin'] = isset($plugin) ? $plugin : false; | |
foreach ($routingPrefixes as $prefix) { | |
if(isset($prefixes) && in_array($prefix,$prefixes)){ | |
$url[$prefix] = true; | |
}else{ | |
$url[$prefix] = false; | |
} | |
} | |
$link = $this->Html->link($title_icon, $url, | |
['escape'=>false, 'class'=>$class]); | |
return "<li class='$class'>" . $link . '</li>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment