Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

177 lines
5.2KB

  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. /**
  40. * @inheritdoc
  41. */
  42. public function behaviors()
  43. {
  44. return [
  45. 'verbs' => [
  46. 'class' => VerbFilter::className(),
  47. 'actions' => [
  48. 'delete' => ['POST'],
  49. ],
  50. ],
  51. ];
  52. }
  53. /**
  54. * Lists all <?= $modelClass ?> models.
  55. * @return mixed
  56. */
  57. public function actionIndex()
  58. {
  59. <?php if (!empty($generator->searchModelClass)): ?>
  60. $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
  61. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  62. return $this->render('index', [
  63. 'searchModel' => $searchModel,
  64. 'dataProvider' => $dataProvider,
  65. ]);
  66. <?php else: ?>
  67. $dataProvider = new ActiveDataProvider([
  68. 'query' => <?= $modelClass ?>::find(),
  69. ]);
  70. return $this->render('index', [
  71. 'dataProvider' => $dataProvider,
  72. ]);
  73. <?php endif; ?>
  74. }
  75. /**
  76. * Displays a single <?= $modelClass ?> model.
  77. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  78. * @return mixed
  79. */
  80. public function actionView(<?= $actionParams ?>)
  81. {
  82. return $this->render('view', [
  83. 'model' => $this->findModel(<?= $actionParams ?>),
  84. ]);
  85. }
  86. /**
  87. * Creates a new <?= $modelClass ?> model.
  88. * If creation is successful, the browser will be redirected to the 'view' page.
  89. * @return mixed
  90. */
  91. public function actionCreate()
  92. {
  93. $model = new <?= $modelClass ?>();
  94. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  95. return $this->redirect(['view', <?= $urlParams ?>]);
  96. } else {
  97. return $this->render('create', [
  98. 'model' => $model,
  99. ]);
  100. }
  101. }
  102. /**
  103. * Updates an existing <?= $modelClass ?> model.
  104. * If update is successful, the browser will be redirected to the 'view' page.
  105. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  106. * @return mixed
  107. */
  108. public function actionUpdate(<?= $actionParams ?>)
  109. {
  110. $model = $this->findModel(<?= $actionParams ?>);
  111. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  112. return $this->redirect(['view', <?= $urlParams ?>]);
  113. } else {
  114. return $this->render('update', [
  115. 'model' => $model,
  116. ]);
  117. }
  118. }
  119. /**
  120. * Deletes an existing <?= $modelClass ?> model.
  121. * If deletion is successful, the browser will be redirected to the 'index' page.
  122. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  123. * @return mixed
  124. */
  125. public function actionDelete(<?= $actionParams ?>)
  126. {
  127. $this->findModel(<?= $actionParams ?>)->delete();
  128. return $this->redirect(['index']);
  129. }
  130. /**
  131. * Finds the <?= $modelClass ?> model based on its primary key value.
  132. * If the model is not found, a 404 HTTP exception will be thrown.
  133. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  134. * @return <?= $modelClass ?> the loaded model
  135. * @throws NotFoundHttpException if the model cannot be found
  136. */
  137. protected function findModel(<?= $actionParams ?>)
  138. {
  139. <?php
  140. if (count($pks) === 1) {
  141. $condition = '$id';
  142. } else {
  143. $condition = [];
  144. foreach ($pks as $pk) {
  145. $condition[] = "'$pk' => \$$pk";
  146. }
  147. $condition = '[' . implode(', ', $condition) . ']';
  148. }
  149. ?>
  150. if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
  151. return $model;
  152. } else {
  153. throw new NotFoundHttpException('The requested page does not exist.');
  154. }
  155. }
  156. }