You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.4 KiB

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