39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
|
|
class MainBreadcrumbs extends Component
|
|
{
|
|
public $pageRoutes = [];
|
|
public $root = false;
|
|
|
|
public function mount($pageRoutes = [], $root = false)
|
|
{
|
|
$this->pageRoutes = $pageRoutes;
|
|
$this->root = $root;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$pathSnippets = array_filter(explode('/', request()->path()));
|
|
$breadcrumbs = $this->root ? [['name' => 'Home', 'url' => '/dashboard']] : $this->buildBreadcrumbs($pathSnippets);
|
|
|
|
return view('livewire.main-breadcrumbs', ['breadcrumbs' => $breadcrumbs]);
|
|
}
|
|
|
|
private function buildBreadcrumbs($pathSnippets)
|
|
{
|
|
$breadcrumbs = [['name' => 'Home', 'url' => '/dashboard']];
|
|
foreach ($pathSnippets as $index => $snippet) {
|
|
$url = '/' . implode('/', array_slice($pathSnippets, 0, $index + 1));
|
|
$routeMatch = array_filter($this->pageRoutes, fn($route) => $route['path'] === $url);
|
|
if ($routeMatch) {
|
|
$route = array_shift($routeMatch);
|
|
$breadcrumbs[] = ['name' => $route['name'], 'url' => $url];
|
|
}
|
|
}
|
|
return $breadcrumbs;
|
|
}
|
|
} |