NavController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\Nav;
  4. use common\models\NavItem;
  5. use Yii;
  6. use yii\data\ActiveDataProvider;
  7. use yii\filters\VerbFilter;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. /**
  11. * NavController implements the CRUD actions for Nav model.
  12. */
  13. class NavController extends Controller
  14. {
  15. public function behaviors()
  16. {
  17. return [
  18. 'verbs' => [
  19. 'class' => VerbFilter::className(),
  20. 'actions' => [
  21. 'delete' => ['post']
  22. ],
  23. ],
  24. ];
  25. }
  26. /**
  27. * Lists all Nav models.
  28. * @return mixed
  29. */
  30. public function actionIndex()
  31. {
  32. $dataProvider = new ActiveDataProvider([
  33. 'query' => Nav::find()
  34. ]);
  35. return $this->render('index', [
  36. 'dataProvider' => $dataProvider,
  37. ]);
  38. }
  39. /**
  40. * Creates a new Nav model.
  41. * If creation is successful, the browser will be redirected to the 'view' page.
  42. * @return mixed
  43. */
  44. public function actionCreate()
  45. {
  46. $model = new Nav();
  47. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  48. return $this->redirect(['update', 'id' => $model->id]);
  49. } else {
  50. return $this->render('create', [
  51. 'model' => $model,
  52. ]);
  53. }
  54. }
  55. /**
  56. * Updates an existing Nav model.
  57. * If update is successful, the browser will be redirected to the 'view' page.
  58. * @param integer $id
  59. * @return mixed
  60. */
  61. public function actionUpdate($id)
  62. {
  63. $model = $this->findModel($id);
  64. $navItemsProvider = new ActiveDataProvider([
  65. 'query' => NavItem::find()->where(['nav_id'=>$model->id])->orderBy('order')
  66. ]);
  67. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  68. return $this->redirect(['index']);
  69. } else {
  70. return $this->render('update', [
  71. 'model' => $model,
  72. 'navItemsProvider'=>$navItemsProvider
  73. ]);
  74. }
  75. }
  76. /**
  77. * Deletes an existing Nav model.
  78. * If deletion is successful, the browser will be redirected to the 'index' page.
  79. * @param integer $id
  80. * @return mixed
  81. */
  82. public function actionDelete($id)
  83. {
  84. $this->findModel($id)->delete();
  85. return $this->redirect(['index']);
  86. }
  87. /**
  88. * Finds the Nav model based on its primary key value.
  89. * If the model is not found, a 404 HTTP exception will be thrown.
  90. * @param integer $id
  91. * @return Nav the loaded model
  92. * @throws NotFoundHttpException if the model cannot be found
  93. */
  94. protected function findModel($id)
  95. {
  96. if (($model = Nav::findOne($id)) !== null) {
  97. return $model;
  98. } else {
  99. throw new NotFoundHttpException('The requested page does not exist.');
  100. }
  101. }
  102. }