Skip to content
112 changes: 62 additions & 50 deletions src/generators/crud/default/controller.php
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' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why renaming the key?

Copy link
Contributor Author

@WinterSilence WinterSilence Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark because AccessControl too have verbs + key = class - easy rule doing code more readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's AccessControl related? We're not using it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related via framework

Copy link
Member

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.

'class' => VerbFilter::class<?= $isPhp7 ? '' : 'Name()' ?>,
'actions' => [
'index' => ['GET'],
'view' => ['GET'],
'create' => ['GET', 'POST'],
'update' => ['GET', 'POST'],
'delete' => ['POST'],
],
],
]
]
]
);
}
Expand All @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous formatting was OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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(
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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting was alright.

<?php endif; ?>
}

Expand All @@ -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 ?>)]);
Copy link
Member

Choose a reason for hiding this comment

The 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()
Expand All @@ -130,15 +143,14 @@ public function actionCreate()
$model->loadDefaultValues();
}

return $this->render('create', [
'model' => $model,
]);
return $this->render('create', ['model' => $model]);
}

/**
* 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
*/
Expand All @@ -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]);
}

/**
* 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();

Expand All @@ -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 = [];
Expand All @@ -185,10 +196,11 @@ protected function findModel(<?= $actionParams ?>)
}
$condition = '[' . implode(', ', $condition) . ']';
?>
if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was OK as is. No need for change here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark it's less readable

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}