1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace backend\controllers;
use Yii;
use common\models\Menu;
use common\helpers\ArrayHelper;
use common\models\search\MenuSearch;
use yii\web\NotFoundHttpException;
/**
* 后台菜单控制器
* @author libingle
*/
class MenuController extends BaseController
{
/**
* ---------------------------------------
* 列表页
* ---------------------------------------
*/
public function actionIndex()
{
/* 添加当前位置到cookie供后续跳转调用*/
$this->setForward();
$menu = new Menu();
$searchModel = new MenuSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'menu' => $menu,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* ---------------------------------------
* 添加
* ---------------------------------------
*/
public function actionAdd()
{
$pid = Yii::$app->request->get('pid', 0);
$model = $this->findModel(0);
if (Yii::$app->request->isPost) {
/* 表单验证 */
$data = Yii::$app->request->post('Menu');
$data['status'] = 1;
if ($this->saveRow($model, $data)) {
$this->success('操作成功', $this->getForward());
} else {
$this->error('操作错误');
}
}
/* 设置默认值 */
$model->loadDefaultValues();
$model->pid = $pid;
/* 渲染模板 */
return $this->render('edit', [
'model' => $model,
]);
}
/**
* ---------------------------------------
* 编辑
* ---------------------------------------
*/
public function actionEdit()
{
$id = Yii::$app->request->get('id', 0);
$model = $this->findModel($id);
if (Yii::$app->request->isPost) {
/* 表单验证 */
$data = Yii::$app->request->post('Menu');
if ($this->saveRow($model, $data)) {
$this->success('操作成功', $this->getForward());
} else {
$this->error('操作错误');
}
}
/* 渲染模板 */
return $this->render('edit', [
'model' => $model,
]);
}
/**
* ---------------------------------------
* 删除或批量删除
* ---------------------------------------
*/
public function actionDelete()
{
$model = $this->findModel(0);
if ($this->delRow($model, 'id')) {
$this->success('删除成功', $this->getForward());
} else {
$this->error('删除失败!');
}
}
/**
* Finds the Article model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Menu the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if ($id == 0) {
return new Menu();
}
if (($model = Menu::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* ---------------------------------------
* 菜单树
* ---------------------------------------
*/
public function actionTree()
{
$lists = Menu::find()->orderBy('sort asc')->where(['hide' => 0])->asArray()->all();
$lists = ArrayHelper::list_to_tree($lists, 'id', 'pid');
$lists = ArrayHelper::jstree($lists);
echo json_encode($lists);
}
/**
* [刷新菜单]
* @author: libingke
*/
public function actionFlush()
{
Menu::flushMenu();
return $this->goBack(Yii::$app->request->getReferrer());
}
}