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.

controller.php 5.2KB

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