controller.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. use yii\db\ActiveRecordInterface;
  3. use yii\helpers\StringHelper;
  4. /**
  5. * This is the template for generating a CRUD controller class file.
  6. *
  7. * @var yii\web\View $this
  8. * @var yii\gii\generators\crud\Generator $generator
  9. */
  10. $controllerClass = StringHelper::basename($generator->controllerClass);
  11. $modelClass = StringHelper::basename($generator->modelClass);
  12. $searchModelClass = StringHelper::basename($generator->searchModelClass);
  13. if ($modelClass === $searchModelClass) {
  14. $searchModelAlias = $searchModelClass . 'Search';
  15. }
  16. /** @var ActiveRecordInterface $class */
  17. $class = $generator->modelClass;
  18. $pks = $class::primaryKey();
  19. $urlParams = $generator->generateUrlParams();
  20. $actionParams = $generator->generateActionParams();
  21. $actionParamComments = $generator->generateActionParamComments();
  22. echo "<?php\n";
  23. ?>
  24. namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;
  25. use Yii;
  26. use <?= ltrim($generator->modelClass, '\\') ?>;
  27. use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
  28. use <?= ltrim($generator->baseControllerClass, '\\') ?>;
  29. use yii\web\NotFoundHttpException;
  30. use yii\filters\VerbFilter;
  31. /**
  32. * <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
  33. */
  34. class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
  35. {
  36. public function behaviors()
  37. {
  38. return [
  39. 'verbs' => [
  40. 'class' => VerbFilter::className(),
  41. 'actions' => [
  42. 'delete' => ['post'],
  43. ],
  44. ],
  45. ];
  46. }
  47. /**
  48. * Lists all <?= $modelClass ?> models.
  49. * @return mixed
  50. */
  51. public function actionIndex()
  52. {
  53. $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>;
  54. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
  55. return $this->render('index', [
  56. 'dataProvider' => $dataProvider,
  57. 'searchModel' => $searchModel,
  58. ]);
  59. }
  60. /**
  61. * Displays a single <?= $modelClass ?> model.
  62. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  63. * @return mixed
  64. */
  65. public function actionView(<?= $actionParams ?>)
  66. {
  67. return $this->render('view', [
  68. 'model' => $this->findModel(<?= $actionParams ?>),
  69. ]);
  70. }
  71. /**
  72. * Creates a new <?= $modelClass ?> model.
  73. * If creation is successful, the browser will be redirected to the 'view' page.
  74. * @return mixed
  75. */
  76. public function actionCreate()
  77. {
  78. $model = new <?= $modelClass ?>;
  79. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  80. return $this->redirect(['view', <?= $urlParams ?>]);
  81. } else {
  82. return $this->render('create', [
  83. 'model' => $model,
  84. ]);
  85. }
  86. }
  87. /**
  88. * Updates an existing <?= $modelClass ?> model.
  89. * If update is successful, the browser will be redirected to the 'view' page.
  90. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  91. * @return mixed
  92. */
  93. public function actionUpdate(<?= $actionParams ?>)
  94. {
  95. $model = $this->findModel(<?= $actionParams ?>);
  96. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  97. return $this->redirect(['view', <?= $urlParams ?>]);
  98. } else {
  99. return $this->render('update', [
  100. 'model' => $model,
  101. ]);
  102. }
  103. }
  104. /**
  105. * Deletes an existing <?= $modelClass ?> model.
  106. * If deletion is successful, the browser will be redirected to the 'index' page.
  107. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  108. * @return mixed
  109. */
  110. public function actionDelete(<?= $actionParams ?>)
  111. {
  112. $this->findModel(<?= $actionParams ?>)->delete();
  113. return $this->redirect(['index']);
  114. }
  115. /**
  116. * Finds the <?= $modelClass ?> model based on its primary key value.
  117. * If the model is not found, a 404 HTTP exception will be thrown.
  118. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  119. * @return <?= $modelClass ?> the loaded model
  120. * @throws NotFoundHttpException if the model cannot be found
  121. */
  122. protected function findModel(<?= $actionParams ?>)
  123. {
  124. <?php
  125. if (count($pks) === 1) {
  126. $condition = '$id';
  127. } else {
  128. $condition = [];
  129. foreach ($pks as $pk) {
  130. $condition[] = "'$pk' => \$$pk";
  131. }
  132. $condition = '[' . implode(', ', $condition) . ']';
  133. }
  134. ?>
  135. if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
  136. return $model;
  137. } else {
  138. throw new NotFoundHttpException('The requested page does not exist.');
  139. }
  140. }
  141. }