1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\page\controllers; |
4
|
|
|
|
5
|
|
|
use app\modules\user\models\User; |
6
|
|
|
use Yii; |
7
|
|
|
use app\modules\page\models\Page; |
8
|
|
|
use app\modules\page\models\PageSearch; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
use yii\web\Controller; |
11
|
|
|
use yii\web\NotFoundHttpException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ManagementController implements the CRUD actions for Page model. |
15
|
|
|
*/ |
16
|
|
|
class ManagementController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
4 |
|
public function behaviors() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'access' => [ |
25
|
4 |
|
'class' => AccessControl::className(), |
26
|
|
|
'rules' => [ |
27
|
|
|
[ |
28
|
|
|
'allow' => true, |
29
|
|
|
'actions' => ['index', 'view', 'update'], |
30
|
|
|
'roles' => [User::ROLE_ADMIN], |
31
|
|
|
], |
32
|
|
|
] |
33
|
|
|
] |
34
|
4 |
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Lists all Page models. |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
1 |
View Code Duplication |
public function actionIndex() |
|
|
|
|
42
|
|
|
{ |
43
|
1 |
|
$searchModel = new PageSearch(); |
44
|
1 |
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
45
|
|
|
|
46
|
1 |
|
return $this->render('index', [ |
47
|
1 |
|
'searchModel' => $searchModel, |
48
|
1 |
|
'dataProvider' => $dataProvider, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Displays a single Page model. |
54
|
|
|
* @param integer $id |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
2 |
|
public function actionView($id) |
58
|
|
|
{ |
59
|
2 |
|
return $this->render('view', [ |
60
|
2 |
|
'model' => $this->findModel($id), |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Updates an existing Page model. |
66
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
67
|
|
|
* @param integer $id |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
2 |
View Code Duplication |
public function actionUpdate($id) |
|
|
|
|
71
|
|
|
{ |
72
|
2 |
|
$model = $this->findModel($id); |
73
|
|
|
|
74
|
1 |
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
75
|
1 |
|
return $this->redirect(['view', 'id' => $model->id]); |
76
|
|
|
} |
77
|
1 |
|
return $this->render('update', [ |
78
|
1 |
|
'model' => $model, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Finds the Page model based on its primary key value. |
84
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
85
|
|
|
* @param integer $id |
86
|
|
|
* @return Page the loaded model |
87
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
88
|
|
|
*/ |
89
|
3 |
|
protected function findModel($id) |
90
|
|
|
{ |
91
|
3 |
|
if (($model = Page::findOne($id)) !== null) { |
92
|
2 |
|
return $model; |
93
|
|
|
} else { |
94
|
1 |
|
throw new NotFoundHttpException('The requested page does not exist.'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.