1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\post\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use zacksleo\yii2\post\models\Post; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PostController implements the CRUD actions for Post model. |
14
|
|
|
*/ |
15
|
|
|
class DefaultController extends Controller |
16
|
|
|
{ |
17
|
|
|
//todo 前缀名要修改 |
18
|
|
|
public function actions() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
'upload' => [ |
22
|
|
|
'class' => 'kucha\ueditor\UEditorAction', |
23
|
|
|
'config' => [ |
24
|
|
|
"imageUrlPrefix" => Yii::$app->request->hostInfo,//图片访问路径前缀 |
25
|
|
|
"imagePathFormat" => "/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", //上传保存路径 |
26
|
|
|
"imageRoot" => Yii::getAlias("@webroot"), |
27
|
|
|
], |
28
|
|
|
] |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Lists all Post models. |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function actionIndex() |
37
|
|
|
{ |
38
|
|
|
$dataProvider = new ActiveDataProvider([ |
39
|
|
|
'query' => Post::find(), |
40
|
|
|
'sort' => [ |
41
|
|
|
'defaultOrder' => [ |
42
|
|
|
'created_at' => SORT_DESC |
43
|
|
|
] |
44
|
|
|
] |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
return $this->render('index', [ |
48
|
|
|
'dataProvider' => $dataProvider, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Displays a single Post model. |
54
|
|
|
* @param integer $id |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function actionView($id) |
58
|
|
|
{ |
59
|
|
|
return $this->render('view', [ |
60
|
|
|
'model' => $this->findModel($id), |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a new Post model. |
66
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function actionCreate() |
70
|
|
|
{ |
71
|
|
|
$model = new Post(); |
72
|
|
|
$model->setScenario('insert'); |
73
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
74
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
75
|
|
|
} else { |
76
|
|
|
return $this->render('create', [ |
77
|
|
|
'model' => $model, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Updates an existing Post model. |
84
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
85
|
|
|
* @param integer $id |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function actionUpdate($id) |
89
|
|
|
{ |
90
|
|
|
$model = $this->findModel($id); |
91
|
|
|
$model->setScenario('update'); |
92
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
93
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
94
|
|
|
} else { |
95
|
|
|
return $this->render('update', [ |
96
|
|
|
'model' => $model, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Deletes an existing Post model. |
103
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
104
|
|
|
* @param integer $id |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function actionDelete($id) |
108
|
|
|
{ |
109
|
|
|
$this->findModel($id)->delete(); |
110
|
|
|
|
111
|
|
|
return $this->redirect(['index']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Finds the Post model based on its primary key value. |
116
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
117
|
|
|
* @param integer $id |
118
|
|
|
* @return Post the loaded model |
119
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
120
|
|
|
*/ |
121
|
|
|
protected function findModel($id) |
122
|
|
|
{ |
123
|
|
|
if (($model = Post::findOne($id)) !== null) { |
124
|
|
|
return $model; |
125
|
|
|
} else { |
126
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @brief 修改文章状态1显示:0不显示 |
133
|
|
|
* @param $id |
134
|
|
|
* @param $status |
135
|
|
|
* @return |
136
|
|
|
*/ |
137
|
|
|
public function actionStatus($id, $status) |
138
|
|
|
{ |
139
|
|
|
$model = $this->findModel($id); |
140
|
|
|
$model->status = $status; |
|
|
|
|
141
|
|
|
$model->save(); |
142
|
|
|
if ($model !== false) { |
143
|
|
|
$message = ['status' => 1, 'message' => "修改成功"]; |
144
|
|
|
} else { |
145
|
|
|
$message = ['status' => 0, 'message' => "修改失败"]; |
146
|
|
|
} |
147
|
|
|
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; |
148
|
|
|
return $message; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.