CategoryController.php 3.1 KB

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