@@ -44,13 +44,13 @@ Let's consider we want to make a list of recent news and reuse it in several vie
44
44
45
45
First of all we can create a Widget class using the artisan command provided by the package.
46
46
``` bash
47
- php artisan make:widget RecentNews --view
47
+ php artisan make:widget RecentNews
48
48
```
49
49
This command generates two files:
50
50
51
51
1 ) ` resources/views/widgets/recent_news.blade.php ` is an empty view.
52
52
53
- Omit "--view " option if you do not need it .
53
+ Add "--plain " option if you do not need a view .
54
54
55
55
2 ) ` app/Widgets/RecentNews ` is a widget class.
56
56
@@ -63,15 +63,24 @@ use Arrilot\Widgets\AbstractWidget;
63
63
64
64
class RecentNews extends AbstractWidget
65
65
{
66
+ /**
67
+ * The configuration array.
68
+ *
69
+ * @var array
70
+ */
71
+ protected $config = [];
72
+
66
73
/**
67
74
* Treat this method as a controller action.
68
- * Return a view or other content to display.
75
+ * Return view() or other content to display.
69
76
*/
70
77
public function run()
71
78
{
72
79
//
73
80
74
- return view("widgets.recent_news");
81
+ return view("widgets.recent_news", [
82
+ 'config' => $this->config,
83
+ ]);
75
84
}
76
85
}
77
86
```
@@ -113,13 +122,13 @@ class RecentNews extends AbstractWidget {
113
122
}
114
123
115
124
...
116
- @widget('recentNews')
125
+ @widget('recentNews') // shows 5
117
126
...
118
- @widget('recentNews', ['count' => 10])
127
+ @widget('recentNews', ['count' => 10]) // shows 10
119
128
```
120
- ` ['count' => 10] ` is a config array.
129
+ ` ['count' => 10] ` is a config array that can be accessed by $this->config .
121
130
122
- Note: no config fields that are not specified when you call the widget are overwritten.
131
+ Note: Config fields that are not specified when you call the widget aren't overwritten:
123
132
124
133
``` php
125
134
class RecentNews extends AbstractWidget {
@@ -135,11 +144,11 @@ class RecentNews extends AbstractWidget {
135
144
@widget('recentNews', ['count' => 10]) // $this->config('foo') is still 'bar'
136
145
```
137
146
138
- Config array is available in all widget methods so you can use it to configure placeholder and container too.
147
+ Config array is available in all widget methods so you can use it to configure placeholder and container too (see below)
139
148
140
149
### Directly
141
150
142
- You can also choose to pass additional parameters to ` run() ` method directly if you like it .
151
+ You can also choose to pass additional parameters to ` run() ` method directly.
143
152
144
153
``` php
145
154
@widget('recentNews', ['count' => 10], 'date', 'asc')
@@ -161,7 +170,7 @@ For example, if you've got dozens of widgets it makes sense to group them in nam
161
170
162
171
You have two ways to call those widgets:
163
172
164
- 1 ) You can pass the full widget name to the ` run ` method.
173
+ 1 ) You can pass the full widget name from the ` default_namespace ` (basically ` App\Widgets ` ) to the ` run ` method.
165
174
``` php
166
175
@widget('News\RecentNews', $config)
167
176
{!! Widget::run('News\RecentNews', $config) !!}
@@ -230,8 +239,8 @@ Consider using web sockets too but they are waaaay harder to set up on the other
230
239
231
240
## Container
232
241
233
- Async and Reloadable widgets both require some DOM interaction so they wrap all widget output in a Container .
234
- This container is defined by AbstractWidget::container() method and can be customized
242
+ Async and Reloadable widgets both require some DOM interaction so they wrap all widget output in a container .
243
+ This container is defined by AbstractWidget::container() method and can be customized therefore.
235
244
236
245
``` php
237
246
/**
0 commit comments