pmtpenza22

Untitled

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