-
-
Notifications
You must be signed in to change notification settings - Fork 192
Update view generators/crud/default/controller
#469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f2d76c4
979404f
c30b544
d20d844
33d2186
b2eafbc
2f41260
b6ae026
5c91607
9ed4adc
33aabb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,73 @@ | ||
| <?php | ||
| /** | ||
| * This is the template for generating a CRUD controller class file. | ||
| * | ||
| * @var \yii\web\View $this | ||
| * @var \yii\gii\generators\crud\Generator $generator | ||
| */ | ||
|
|
||
| use yii\db\ActiveRecordInterface; | ||
| use yii\helpers\StringHelper; | ||
|
|
||
|
|
||
| /** @var yii\web\View $this */ | ||
| /** @var yii\gii\generators\crud\Generator $generator */ | ||
|
|
||
| $controllerClass = StringHelper::basename($generator->controllerClass); | ||
| $modelClass = StringHelper::basename($generator->modelClass); | ||
| $searchModelClass = StringHelper::basename($generator->searchModelClass); | ||
| if ($modelClass === $searchModelClass) { | ||
| $searchModelAlias = $searchModelClass . 'Search'; | ||
| if ($generator->searchModelClass !== '') { | ||
| $searchModelClass = StringHelper::basename($generator->searchModelClass); | ||
| if ($modelClass === $searchModelClass) { | ||
| $searchModelAlias = $searchModelClass . 'Search'; | ||
| } | ||
| } | ||
|
|
||
| /* @var $class ActiveRecordInterface */ | ||
| $class = $generator->modelClass; | ||
| /** @var $class ActiveRecordInterface */ | ||
| $class = ltrim($generator->modelClass, '\\'); | ||
| $pks = $class::primaryKey(); | ||
| $urlParams = $generator->generateUrlParams(); | ||
| $actionParams = $generator->generateActionParams(); | ||
| $actionParamComments = $generator->generateActionParamComments(); | ||
| $isPhp7 = PHP_MAJOR_VERSION === 7; | ||
|
|
||
| echo "<?php\n"; | ||
| ?> | ||
|
|
||
| namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>; | ||
|
|
||
| use <?= ltrim($generator->modelClass, '\\') ?>; | ||
| use <?= $class ?>; | ||
| <?php if (!empty($generator->searchModelClass)): ?> | ||
| use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>; | ||
| use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : '') ?>; | ||
| <?php else: ?> | ||
| use yii\data\ActiveDataProvider; | ||
| <?php endif; ?> | ||
| use <?= ltrim($generator->baseControllerClass, '\\') ?>; | ||
| use yii\web\Response; | ||
| use yii\web\NotFoundHttpException; | ||
| use yii\filters\VerbFilter; | ||
| use Yii; | ||
|
|
||
| /** | ||
| * <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model. | ||
| * <?= $controllerClass ?> implements the CRUD actions for `<?= $modelClass ?>` model. | ||
| */ | ||
| class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?> | ||
| { | ||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function behaviors() | ||
| public function behaviors()<?= ($isPhp7 ? ': array' : '') . "\n" ?> | ||
| { | ||
| return array_merge( | ||
| parent::behaviors(), | ||
| [ | ||
| 'verbs' => [ | ||
| 'class' => VerbFilter::className(), | ||
| 'verbFilter' => [ | ||
| 'class' => VerbFilter::class<?= $isPhp7 ? '' : 'Name()' ?>, | ||
| 'actions' => [ | ||
| 'index' => ['GET'], | ||
| 'view' => ['GET'], | ||
| 'create' => ['GET', 'POST'], | ||
| 'update' => ['GET', 'POST'], | ||
| 'delete' => ['POST'], | ||
| ], | ||
| ], | ||
| ] | ||
| ] | ||
| ] | ||
| ); | ||
| } | ||
|
|
@@ -67,36 +77,40 @@ public function behaviors() | |
| * | ||
| * @return string | ||
| */ | ||
| public function actionIndex() | ||
| public function actionIndex()<?= ($isPhp7 ? ': string' : '') . "\n" ?> | ||
| { | ||
| <?php if (!empty($generator->searchModelClass)): ?> | ||
| $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>(); | ||
| $dataProvider = $searchModel->search($this->request->queryParams); | ||
|
|
||
| return $this->render('index', [ | ||
| 'searchModel' => $searchModel, | ||
| 'dataProvider' => $dataProvider, | ||
| ]); | ||
| return $this->render( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous formatting was OK.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. psr-12-compatible style. yes, yii uses prev psr style, but we generate code for users
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was PSR-12 compatible. |
||
| 'index', | ||
| [ | ||
| 'searchModel' => $searchModel, | ||
| 'dataProvider' => $dataProvider, | ||
| ] | ||
| ); | ||
| <?php else: ?> | ||
| $dataProvider = new ActiveDataProvider([ | ||
| 'query' => <?= $modelClass ?>::find(), | ||
| /* | ||
| 'pagination' => [ | ||
| 'pageSize' => 50 | ||
| ], | ||
| 'sort' => [ | ||
| 'defaultOrder' => [ | ||
| $dataProvider = Yii::createObject( | ||
samdark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ActiveDataProvider::class<?= $isPhp7 ? '' : 'Name()' ?>, | ||
| [ | ||
| 'query' => <?= $modelClass ?>::find(), | ||
| /* | ||
| 'pagination' => [ | ||
| 'pageSize' => 50 | ||
| ], | ||
| 'sort' => [ | ||
| 'defaultOrder' => [ | ||
| <?php foreach ($pks as $pk): ?> | ||
| <?= "'$pk' => SORT_DESC,\n" ?> | ||
| <?= "'$pk' => SORT_DESC,\n" ?> | ||
| <?php endforeach; ?> | ||
| ] | ||
| ] | ||
| ], | ||
| */ | ||
| ]); | ||
| */ | ||
| ] | ||
| ); | ||
|
|
||
| return $this->render('index', [ | ||
| 'dataProvider' => $dataProvider, | ||
| ]); | ||
| return $this->render('index', ['dataProvider' => $dataProvider]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting was alright. |
||
| <?php endif; ?> | ||
| } | ||
|
|
||
|
|
@@ -106,16 +120,15 @@ public function actionIndex() | |
| * @return string | ||
| * @throws NotFoundHttpException if the model cannot be found | ||
| */ | ||
| public function actionView(<?= $actionParams ?>) | ||
| public function actionView(<?= $actionParams ?>)<?= ($isPhp7 ? ': string' : '') . "\n" ?> | ||
| { | ||
| return $this->render('view', [ | ||
| 'model' => $this->findModel(<?= $actionParams ?>), | ||
| ]); | ||
| return $this->render('view', ['model' => $this->findModel(<?= $actionParams ?>)]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting was alright. |
||
| } | ||
|
|
||
| /** | ||
| * Creates a new <?= $modelClass ?> model. | ||
| * If creation is successful, the browser will be redirected to the 'view' page. | ||
| * @return Response|string | ||
| * @return string|\yii\web\Response | ||
| */ | ||
| public function actionCreate() | ||
|
|
@@ -130,15 +143,14 @@ public function actionCreate() | |
| $model->loadDefaultValues(); | ||
| } | ||
|
|
||
| return $this->render('create', [ | ||
| 'model' => $model, | ||
| ]); | ||
| return $this->render('create', ['model' => $model]); | ||
WinterSilence marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Updates an existing <?= $modelClass ?> model. | ||
| * If update is successful, the browser will be redirected to the 'view' page. | ||
| * <?= implode("\n * ", $actionParamComments) . "\n" ?> | ||
| * @return Response|string | ||
| * @return string|\yii\web\Response | ||
| * @throws NotFoundHttpException if the model cannot be found | ||
| */ | ||
|
|
@@ -150,19 +162,18 @@ public function actionUpdate(<?= $actionParams ?>) | |
| return $this->redirect(['view', <?= $urlParams ?>]); | ||
| } | ||
|
|
||
| return $this->render('update', [ | ||
| 'model' => $model, | ||
| ]); | ||
| return $this->render('update', ['model' => $model]); | ||
WinterSilence marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Deletes an existing <?= $modelClass ?> model. | ||
| * If deletion is successful, the browser will be redirected to the 'index' page. | ||
| * <?= implode("\n * ", $actionParamComments) . "\n" ?> | ||
| * @return Response | ||
| * @return \yii\web\Response | ||
| * @throws NotFoundHttpException if the model cannot be found | ||
| */ | ||
| public function actionDelete(<?= $actionParams ?>) | ||
| public function actionDelete(<?= $actionParams ?>)<?= ($isPhp7 ? ': Response' : '') . "\n" ?> | ||
| { | ||
| $this->findModel(<?= $actionParams ?>)->delete(); | ||
|
|
||
|
|
@@ -176,7 +187,7 @@ public function actionDelete(<?= $actionParams ?>) | |
| * @return <?= $modelClass ?> the loaded model | ||
| * @throws NotFoundHttpException if the model cannot be found | ||
| */ | ||
| protected function findModel(<?= $actionParams ?>) | ||
| protected function findModel(<?= $actionParams ?>)<?= ($isPhp7 ? ': ' . $modelClass : '') . "\n" ?> | ||
| { | ||
| <?php | ||
| $condition = []; | ||
|
|
@@ -185,10 +196,11 @@ protected function findModel(<?= $actionParams ?>) | |
| } | ||
| $condition = '[' . implode(', ', $condition) . ']'; | ||
| ?> | ||
| if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was OK as is. No need for change here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @samdark it's less readable
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's not. That's more of personal preferences. I'd not change it for that reason.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other than that, you've changed early return to late return complicating the code a bit. With this assign in condition I agree. |
||
| return $model; | ||
| $model = <?= $modelClass ?>::findOne(<?= $condition ?>); | ||
| if ($model === null) { | ||
| throw new NotFoundHttpException(<?= $generator->generateString('The requested page does not exist.') ?>); | ||
| } | ||
|
|
||
| throw new NotFoundHttpException(<?= $generator->generateString('The requested page does not exist.') ?>); | ||
| return $model; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why renaming the key?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samdark because
AccessControltoo haveverbs+ key = class - easy rule doing code more readableThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How's
AccessControlrelated? We're not using it here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related via framework
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? That's the name of the behavior registered. There's no another behavior so a longer name doesn't make sense to me.