Skip to content

Instantly share code, notes, and snippets.

@0xMatt
Created June 12, 2014 14:59
Show Gist options
  • Save 0xMatt/78e296af5d99f6aaaa19 to your computer and use it in GitHub Desktop.
Save 0xMatt/78e296af5d99f6aaaa19 to your computer and use it in GitHub Desktop.
My breadcrumb classs
<?php namespace 0xMatt\Breadcrumb;
use View;
class Breadcrumb
{
// ---------------------------------------------
// Class Properties
// ---------------------------------------------
/**
* Crumb container
*
* @var array
*/
protected $crumbs = [];
// ---------------------------------------------
// Class Methods
// ---------------------------------------------
/**
* Crumb maker
*
* @param callable $callback
* @return \0xMatt\Breadcrumb\Breadcrumb
*/
public function make($callback)
{
if(is_callable($callback))
{
call_user_func($callback, $this);
// Make available for all views
View::composer('*', function ($view)
{
$view->with('crumbs', $this);
});
return $this;
}
}
/**
* Render crumbs
*
* @param string $route
* @param string $name
* @return \0xMatt\Breadcrumb\Breadcrumb
*/
public function add($route, $name)
{
$this->crumbs[] = [$route,$name];
return $this;
}
/**
* Render crumbs
*
* @param string $route
* @param string $name
* @return \0xMatt\Breadcrumb\Breadcrumb
*/
public function prepend($route, $name)
{
$crumb = [$route,$name];
array_unshift($this->crumbs, $crumb);
return $this;
}
/**
* Render crumbs
*
* @return string
*/
public function render()
{
$crumbs = '';
$last = array_pop($this->crumbs);
foreach($this->crumbs as $key => $crumb)
{
$crumbs .= '<li><a href="' . url($crumb[0]) . '">' . $crumb[1] . '</a> <i class="fa fa-angle-right"></i></li>';
}
$crumbs .= '<li class="current">' . $last[1] . '</li>';
return $crumbs;
}
/**
* Render crumbs...magically
*
* @return mixed string|object
*/
public function __toString()
{
try
{
return $this->render();
}
catch(\Exception $e)
{
throw new \Exception($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment