CommentController.php 3.1 KB

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