1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\widgets; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Alert widget renders a message from session flash. All flash messages are displayed |
7
|
|
|
* in the sequence they were assigned using setFlash. You can set message as following: |
8
|
|
|
* |
9
|
|
|
* ```php |
10
|
|
|
* \Yii::$app->getSession()->setFlash('error', 'This is the message'); |
11
|
|
|
* \Yii::$app->getSession()->setFlash('success', 'This is the message'); |
12
|
|
|
* \Yii::$app->getSession()->setFlash('info', 'This is the message'); |
13
|
|
|
* ``` |
14
|
|
|
* |
15
|
|
|
* Multiple messages could be set as follows: |
16
|
|
|
* |
17
|
|
|
* ```php |
18
|
|
|
* \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']); |
19
|
|
|
* ``` |
20
|
|
|
* |
21
|
|
|
* @author Kartik Visweswaran <[email protected]> |
22
|
|
|
* @author Alexander Makarov <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Alert extends \yii\bootstrap\Widget |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array the alert types configuration for the flash messages. |
28
|
|
|
* This array is setup as $key => $value, where: |
29
|
|
|
* - $key is the name of the session flash variable |
30
|
|
|
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) |
31
|
|
|
*/ |
32
|
|
|
public $alertTypes = [ |
33
|
|
|
'error' => 'alert-danger', |
34
|
|
|
'danger' => 'alert-danger', |
35
|
|
|
'success' => 'alert-success', |
36
|
|
|
'info' => 'alert-info', |
37
|
|
|
'warning' => 'alert-warning' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array the options for rendering the close button tag. |
42
|
|
|
*/ |
43
|
|
|
public $closeButton = []; |
44
|
|
|
|
45
|
|
|
public function init() |
46
|
|
|
{ |
47
|
|
|
parent::init(); |
48
|
|
|
|
49
|
|
|
$session = \Yii::$app->getSession(); |
|
|
|
|
50
|
|
|
$flashes = $session->getAllFlashes(); |
51
|
|
|
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; |
52
|
|
|
|
53
|
|
|
foreach ($flashes as $type => $data) { |
54
|
|
|
if (isset($this->alertTypes[$type])) { |
55
|
|
|
$data = (array) $data; |
56
|
|
|
foreach ($data as $message) { |
57
|
|
|
/* initialize css class for each alert box */ |
58
|
|
|
$this->options['class'] = $this->alertTypes[$type] . $appendCss; |
59
|
|
|
|
60
|
|
|
/* assign unique id to each alert box */ |
61
|
|
|
$this->options['id'] = $this->getId() . '-' . $type; |
62
|
|
|
|
63
|
|
|
echo \yii\bootstrap\Alert::widget([ |
64
|
|
|
'body' => $message, |
65
|
|
|
'closeButton' => $this->closeButton, |
66
|
|
|
'options' => $this->options, |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$session->removeFlash($type); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: