Skip to content

Instantly share code, notes, and snippets.

@yfktn
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save yfktn/9931431 to your computer and use it in GitHub Desktop.

Select an option

Save yfktn/9931431 to your computer and use it in GitHub Desktop.
Laravel 4 HTML Macro to Generate Breadcrumbs
/**
* Laravel 4.1 HTML Macro to generate breadcrumbs!
*
* Use it to generate breadcrumbs that compatible with Bootstrap 3.
*
* To use it you need to pass value through $arrayLinks with array that contains
* named keys and it value. Key would be the label of breadcrumbs and value
* would be the link. If we don't pass the named key then it would be label without
* link attached.
*
* To customise home you may use "$homeLinks" with array that contains 'link' and
* 'label' as keys with it corresponding value.
*
* Example:
*
* $arrayLinks = array(
* 'Post Index'=>route('post-index'), // breadcrumb element to index
* 'Editing ' . $post->title // last elements of breadcrumbs
* );
*
* $homeLinks = array(
* 'label' => 'Control Panel', // change default index
* 'link' => route('control-panel') // and it link
* );
*
* {{ HTML::breadcrumbs( $arrayLinks, $homeLinks ) }}
*/
HTML::macro('breadcrumbs', function($arrayLinks, $homeLinks=null){
$link = array();
// first generate for home ...
$theHome = array('link'=>'/', 'label'=>'Home');
if($homeLinks!==null) {
if(is_array($homeLinks)) {
$theHome['link'] = !isset($homeLinks['link'])?:$homeLinks['link'];
$theHome['label'] = !isset($homeLinks['label'])?:$homeLinks['label'];
} else {
$theHome['link'] = $homeLinks;
}
}
$link[] = sprintf('<li><a href="%s">%s</a></li>', $theHome['link'], $theHome['label']);
// second lets check all links ...
foreach ($arrayLinks as $key => $value) {
// the key would be label and value would be the link!
if(is_numeric($key)) {
// then it must be label without link ...
$link[] = sprintf('<li>%s</li>',
$value);
} else {
$link[] = sprintf('<li><a href="%s">%s</a></li>',
$value, $key);
}
}
return '<ol class="breadcrumb">'
.implode("", $link)
.'</ol>';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment