Advertisement
pmtpenza22

Untitled

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