NavItemController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\Nav;
  4. use common\models\NavItem;
  5. use Yii;
  6. use yii\filters\VerbFilter;
  7. use yii\helpers\Url;
  8. use yii\web\Controller;
  9. use yii\web\HttpException;
  10. use yii\web\NotFoundHttpException;
  11. /**
  12. * NavItemController implements the CRUD actions for NavItem model.
  13. */
  14. class NavItemController extends Controller
  15. {
  16. public function getViewPath()
  17. {
  18. return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'nav/item';
  19. }
  20. public function actions()
  21. {
  22. return [
  23. 'position' => [
  24. 'class' => 'backend\actions\Position',
  25. 'returnUrl' => function($model){
  26. return Url::to(['/nav/update', 'id' => $model->nav_id]);
  27. }
  28. ],
  29. 'switcher' => 'backend\widgets\grid\SwitcherAction'
  30. ];
  31. }
  32. public function behaviors()
  33. {
  34. return [
  35. 'verbs' => [
  36. 'class' => VerbFilter::className(),
  37. 'actions' => [
  38. 'delete' => ['post'],
  39. ],
  40. ],
  41. ];
  42. }
  43. /**
  44. * Creates a new NavItem model.
  45. * If creation is successful, the browser will be redirected to the 'view' page.
  46. * @return mixed
  47. */
  48. public function actionCreate($nav_id)
  49. {
  50. $model = new NavItem();
  51. $nav = Nav::findOne($nav_id);
  52. if (!$nav) {
  53. throw new HttpException(400);
  54. }
  55. $model->nav_id = $nav->id;
  56. if ($model->load(Yii::$app->request->post())) {
  57. if ($model->save()) {
  58. Yii::$app->getSession()->setFlash('success', '添加成功');
  59. return $this->redirect(['/nav/update', 'id' => $model->nav_id]);
  60. }
  61. }
  62. return $this->render('create', [
  63. 'model' => $model,
  64. 'nav' => $nav,
  65. ]);
  66. }
  67. /**
  68. * Updates an existing NavItem model.
  69. * If update is successful, the browser will be redirected to the 'view' page.
  70. * @param integer $id
  71. * @return mixed
  72. */
  73. public function actionUpdate($id)
  74. {
  75. $model = $this->findModel($id);
  76. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  77. Yii::$app->getSession()->setFlash('success', '更新成功');
  78. return $this->redirect(['/nav/update', 'id' => $model->nav_id]);
  79. }
  80. return $this->render('update', [
  81. 'model' => $model,
  82. ]);
  83. }
  84. /**
  85. * Deletes an existing NavItem model.
  86. * If deletion is successful, the browser will be redirected to the 'index' page.
  87. * @param integer $id
  88. * @return mixed
  89. */
  90. public function actionDelete($id)
  91. {
  92. $model = $this->findModel($id);
  93. if ($model->delete()) {
  94. return $this->redirect(['/nav/update', 'id'=>$model->nav_id]);
  95. };
  96. }
  97. /**
  98. * Finds the NavItem 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 NavItem the loaded model
  102. * @throws NotFoundHttpException if the model cannot be found
  103. */
  104. public function findModel($id)
  105. {
  106. if (($model = NavItem::findOne($id)) !== null) {
  107. return $model;
  108. } else {
  109. throw new NotFoundHttpException('The requested page does not exist.');
  110. }
  111. }
  112. }