PageController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace manager\controllers;
  3. use app\models\Page;
  4. use yii\data\ActiveDataProvider;
  5. use yii\web\Controller;
  6. use yii\web\NotFoundHttpException;
  7. use yii\filters\VerbFilter;
  8. use Yii;
  9. /**
  10. * PageController implements the CRUD actions for Page model.
  11. */
  12. class PageController extends Controller
  13. {
  14. /**
  15. * @inheritDoc
  16. */
  17. public function behaviors()
  18. {
  19. return array_merge(
  20. parent::behaviors(),
  21. [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. ]
  29. );
  30. }
  31. /**
  32. * Lists all Page models.
  33. *
  34. * @return string
  35. */
  36. public function actionIndex()
  37. {
  38. $dataProvider = new ActiveDataProvider([
  39. 'query' => Page::find(),
  40. /*
  41. 'pagination' => [
  42. 'pageSize' => 50
  43. ],
  44. 'sort' => [
  45. 'defaultOrder' => [
  46. 'id' => SORT_DESC,
  47. ]
  48. ],
  49. */
  50. ]);
  51. return $this->render('index', [
  52. 'dataProvider' => $dataProvider,
  53. ]);
  54. }
  55. /**
  56. * Displays a single Page model.
  57. * @param int $id ID
  58. * @return string
  59. * @throws NotFoundHttpException if the model cannot be found
  60. */
  61. public function actionView($id)
  62. {
  63. return $this->render('view', [
  64. 'model' => $this->findModel($id),
  65. ]);
  66. }
  67. /**
  68. * Creates a new Page model.
  69. * If creation is successful, the browser will be redirected to the 'view' page.
  70. * @return string|\yii\web\Response
  71. */
  72. public function actionCreate()
  73. {
  74. $model = new Page();
  75. if ($this->request->isPost) {
  76. if ($model->load($this->request->post()) && $model->save()) {
  77. return $this->redirect(['view', 'id' => $model->id]);
  78. }
  79. } else {
  80. $model->loadDefaultValues();
  81. }
  82. return $this->render('create', [
  83. 'model' => $model,
  84. ]);
  85. }
  86. /**
  87. * Updates an existing Page model.
  88. * If update is successful, the browser will be redirected to the 'view' page.
  89. * @param int $id ID
  90. * @return string|\yii\web\Response
  91. * @throws NotFoundHttpException if the model cannot be found
  92. */
  93. public function actionUpdate($id)
  94. {
  95. $model = $this->findModel($id);
  96. if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
  97. Yii::$app->cache->delete("static_pagesx");
  98. return $this->redirect(['view', 'id' => $model->id]);
  99. }
  100. return $this->render('update', [
  101. 'model' => $model,
  102. ]);
  103. }
  104. /**
  105. * Deletes an existing Page model.
  106. * If deletion is successful, the browser will be redirected to the 'index' page.
  107. * @param int $id ID
  108. * @return \yii\web\Response
  109. * @throws NotFoundHttpException if the model cannot be found
  110. */
  111. public function actionDelete($id)
  112. {
  113. $this->findModel($id)->delete();
  114. return $this->redirect(['index']);
  115. }
  116. /**
  117. * Finds the Page model based on its primary key value.
  118. * If the model is not found, a 404 HTTP exception will be thrown.
  119. * @param int $id ID
  120. * @return Page the loaded model
  121. * @throws NotFoundHttpException if the model cannot be found
  122. */
  123. protected function findModel($id)
  124. {
  125. if (($model = Page::findOne(['id' => $id])) !== null) {
  126. return $model;
  127. }
  128. throw new NotFoundHttpException('The requested page does not exist.');
  129. }
  130. }