AdminController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace plugins\donation\controllers;
  3. use Yii;
  4. use plugins\donation\models\Donation;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. /**
  10. * 捐赠管理
  11. */
  12. class AdminController 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. * 捐赠列表
  27. * @return mixed
  28. */
  29. public function actionIndex()
  30. {
  31. $dataProvider = new ActiveDataProvider([
  32. 'query' => Donation::find(),
  33. 'sort' => [
  34. 'defaultOrder' => [
  35. 'id' => SORT_DESC
  36. ]
  37. ]
  38. ]);
  39. return $this->render('index', [
  40. 'dataProvider' => $dataProvider,
  41. ]);
  42. }
  43. /**
  44. * 捐赠详情
  45. * @param integer $id
  46. * @return mixed
  47. */
  48. public function actionView($id)
  49. {
  50. return $this->render('view', [
  51. 'model' => $this->findModel($id),
  52. ]);
  53. }
  54. /**
  55. * 新建捐赠
  56. * @return mixed
  57. */
  58. public function actionCreate()
  59. {
  60. $model = new Donation();
  61. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  62. return $this->redirect(['view', 'id' => $model->id]);
  63. } else {
  64. return $this->render('create', [
  65. 'model' => $model,
  66. ]);
  67. }
  68. }
  69. /**
  70. * 编辑捐赠
  71. * @param integer $id
  72. * @return mixed
  73. */
  74. public function actionUpdate($id)
  75. {
  76. $model = $this->findModel($id);
  77. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  78. return $this->redirect(['view', 'id' => $model->id]);
  79. } else {
  80. return $this->render('update', [
  81. 'model' => $model,
  82. ]);
  83. }
  84. }
  85. /**
  86. * 删除捐赠
  87. * @param integer $id
  88. * @return mixed
  89. */
  90. public function actionDelete($id)
  91. {
  92. $this->findModel($id)->delete();
  93. return $this->redirect(['index']);
  94. }
  95. /**
  96. * Finds the Donation model based on its primary key value.
  97. * If the model is not found, a 404 HTTP exception will be thrown.
  98. * @param integer $id
  99. * @return Donation the loaded model
  100. * @throws NotFoundHttpException if the model cannot be found
  101. */
  102. protected function findModel($id)
  103. {
  104. if (($model = Donation::findOne($id)) !== null) {
  105. return $model;
  106. } else {
  107. throw new NotFoundHttpException('The requested page does not exist.');
  108. }
  109. }
  110. }