CategoryController.php 3.1 KB

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