PostsController.php 3.0 KB

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