PageController.php 3.1 KB

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