|
<?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()); |
|
} |
|
} |
|
} |