|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\backend; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @link http://www.diemeisterei.de/ |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) 2014 diemeisterei GmbH, Stuttgart |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Module. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Tobias Munk <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Module extends \yii\base\Module |
|
22
|
|
|
{ |
|
23
|
|
|
public function getMenuItems() |
|
24
|
|
|
{ |
|
25
|
|
|
$menuItemPresets = [ |
|
26
|
|
|
'admin' => ['label' => '<i class="fa fa-dashboard"></i> <span>Dashboard</span>', 'url' => ['/admin']], |
|
27
|
|
|
'user' => ['label' => '<i class="fa fa-users"></i> <span>Users</span>', 'url' => ['/user/admin']], |
|
28
|
|
|
'packaii' => ['label' => '<i class="fa fa-cubes"></i> <span>Packages</span>', 'url' => ['/packaii']], |
|
29
|
|
|
'gii' => ['label' => '<i class="fa fa-code"></i> <span>Code Generation</span>', 'url' => ['/gii']], |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
$autoMenuItems = []; |
|
33
|
|
|
foreach (\Yii::$app->getModules() as $name => $m) { |
|
34
|
|
|
switch ($name) { |
|
35
|
|
|
case 'admin': |
|
36
|
|
|
case 'user': |
|
37
|
|
|
case 'packaii': |
|
38
|
|
|
case 'gii': |
|
39
|
|
|
$menuItems[] = $menuItemPresets[$name]; |
|
|
|
|
|
|
40
|
|
|
break; |
|
41
|
|
|
default: |
|
42
|
|
|
$module = \Yii::$app->getModule($name); |
|
43
|
|
|
$autoMenuItems[] = [ |
|
44
|
|
|
'label' => '<i class="fa fa-cube"></i> <span>'.ucfirst($name).'</span>', |
|
45
|
|
|
'url' => ['/'.$module->id], |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$menuItems = ArrayHelper::merge($menuItems, $autoMenuItems); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
return $menuItems; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getControllers($module = null) |
|
56
|
|
|
{ |
|
57
|
|
|
if ($module === null) { |
|
58
|
|
|
$module = \Yii::$app; |
|
59
|
|
|
} else { |
|
60
|
|
|
$module = \Yii::$app->getModule($module); |
|
61
|
|
|
} |
|
62
|
|
|
foreach (scandir($module->getControllerPath()) as $i => $name) { |
|
63
|
|
|
if (substr($name, 0, 1) == '.') { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
$controllers[] = \yii\helpers\Inflector::camel2id(str_replace('Controller.php', '', $name)); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $controllers; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.