i have a question about dynamic breadcrumbs in laravel 5.4 . i don't want use package for this so my question is that:
i write a basecontroller like this
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class BaseController extends Controller
{
private $breadcrumbs;
function __construct()
{
$this->breadcrumbs[] = [
'title' => 'Home',
'type' => 'route',
'href' => 'admin.dashboard'
];
view()->share('breadcrumbs', $this->breadcrumbs);
}
}
and i use it in child controller like this
<?php
namespace App\Http\Controllers\Admin\Management;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\BaseController;
class PackageController extends BaseController
{
function __construct()
{
parent::__construct();
}
public function getList()
{
$this->breadcrumbs[] = [
'title' => 'Packages',
'type' => 'route',
'href' => 'admin.package.list',
'class' => 'active'
];
return view('admin.layouts.package.list');
}
i want use this breadcrumbs variable in breadcrumbs.blade.php partial in master page
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
@include('admin.layouts.shared.breadcrumb')
</body>
</html>
breadcrumbs partial view renders an ul list has multiple li>a tags. but this is not working now.
1 . why it doesn't work ?
2 . how can add a new breadcrumb array element to breadcrumb variable in basecontroller and render full parts with added last element ?
please help me ?
( sorry my bad english )
The parent constructor runs before the getList()
function. This means that the following has already run:
view()->share('breadcrumbs', $this->breadcrumbs);
I'm pretty sure it makes a copy of the breadcrumbs array when you share it, not leaving a reference to it so you can modify it as you go.
Why not make a method in the base controller like:
public function setBreadcrumbs($breadcrumbs)
{
$this->breadcrumbs = $breadcrumbs;
view()->share('breadcrumbs', $this->breadcrumbs);
}
And then in getList()
call it like this:
$this->setBreadcrumbs([
'title' => 'Packages',
'type' => 'route',
'href' => 'admin.package.list',
'class' => 'active'
]);