1
|
|
|
<?php namespace Arcanedev\Breadcrumbs\Traits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Breadcrumbs\Builder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait BreadcrumbsTrait |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\Breadcrumbs\Traits |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
trait BreadcrumbsTrait |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** |
18
|
|
|
* Breadcrumbs container name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $breadcrumbsContainer = 'public'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Breadcrumbs items collection. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $breadcrumbsItems = []; |
30
|
|
|
|
31
|
|
|
/* ------------------------------------------------------------------------------------------------ |
32
|
|
|
| Getters & Setters |
33
|
|
|
| ------------------------------------------------------------------------------------------------ |
34
|
|
|
*/ |
35
|
|
|
/** |
36
|
|
|
* Set breadcrumbs container name. |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
4 |
|
protected function setBreadcrumbsContainer($name) |
43
|
|
|
{ |
44
|
4 |
|
$this->breadcrumbsContainer = $name; |
45
|
|
|
|
46
|
4 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the breadcrumbs home item (root). |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
4 |
|
protected function getBreadcrumbsHomeItem() |
55
|
|
|
{ |
56
|
4 |
|
$route = config('breadcrumbs.home-route', 'public::home'); |
57
|
|
|
|
58
|
|
|
return [ |
59
|
4 |
|
'title' => 'Home', |
60
|
4 |
|
'url' => route($route), |
61
|
3 |
|
'data' => [], |
62
|
3 |
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/* ------------------------------------------------------------------------------------------------ |
66
|
|
|
| Main Functions |
67
|
|
|
| ------------------------------------------------------------------------------------------------ |
68
|
|
|
*/ |
69
|
|
|
/** |
70
|
|
|
* Register a breadcrumb. |
71
|
|
|
* |
72
|
|
|
* @param string $container |
73
|
|
|
* @param array $item |
74
|
|
|
*/ |
75
|
4 |
|
protected function registerBreadcrumbs($container, array $item = []) |
76
|
|
|
{ |
77
|
4 |
|
$this->setBreadcrumbsContainer($container); |
78
|
|
|
|
79
|
|
|
breadcrumbs()->register('main', function(Builder $bc) use ($item) { |
80
|
4 |
|
if (empty($item)) { |
81
|
4 |
|
$item = $this->getBreadcrumbsHomeItem(); |
|
|
|
|
82
|
3 |
|
} |
83
|
|
|
|
84
|
4 |
|
$bc->push($item['title'], $item['url'], isset($item['array']) ? $item['array'] : []); |
85
|
4 |
|
}); |
86
|
4 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Load all breadcrumbs. |
90
|
|
|
*/ |
91
|
|
|
protected function loadBreadcrumbs() |
92
|
|
|
{ |
93
|
4 |
|
breadcrumbs()->register($this->breadcrumbsContainer, function(Builder $bc) { |
94
|
4 |
|
$bc->parent('main'); |
95
|
|
|
|
96
|
4 |
|
if (empty($this->breadcrumbsItems)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
foreach ($this->breadcrumbsItems as $crumb) { |
101
|
4 |
|
$bc->push($crumb['title'], $crumb['url'], isset($crumb['array']) ? $crumb['array'] : []); |
102
|
3 |
|
} |
103
|
4 |
|
}); |
104
|
4 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add breadcrumb. |
108
|
|
|
* |
109
|
|
|
* @param string $title |
110
|
|
|
* @param string $url |
111
|
|
|
* @param array $data |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
4 |
|
protected function addBreadcrumb($title, $url = '', array $data = []) |
116
|
|
|
{ |
117
|
4 |
|
$this->breadcrumbsItems[] = compact('title', 'url', 'data'); |
118
|
|
|
|
119
|
4 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add breadcrumb with route. |
124
|
|
|
* |
125
|
|
|
* @param string $title |
126
|
|
|
* @param string $route |
127
|
|
|
* @param array $parameters |
128
|
|
|
* @param array $data |
129
|
|
|
* |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
4 |
|
protected function addBreadcrumbRoute($title, $route, array $parameters = [], array $data = []) |
133
|
|
|
{ |
134
|
4 |
|
return $this->addBreadcrumb($title, route($route, $parameters), $data); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope