controller.php 4.33 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2

3
use yii\db\ActiveRecordInterface;
Qiang Xue committed
4 5
use yii\helpers\StringHelper;

Qiang Xue committed
6
/**
Qiang Xue committed
7 8
 * This is the template for generating a CRUD controller class file.
 *
Alexander Makarov committed
9
 * @var yii\web\View $this
Qiang Xue committed
10
 * @var yii\gii\generators\crud\Generator $generator
Qiang Xue committed
11
 */
Qiang Xue committed
12

Qiang Xue committed
13 14 15
$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
16 17 18
if ($modelClass === $searchModelClass) {
	$searchModelAlias = $searchModelClass.'Search';
}
Qiang Xue committed
19

20 21 22
/** @var ActiveRecordInterface $class */
$class = $generator->modelClass;
$pks = $class::primaryKey();
Qiang Xue committed
23 24 25
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();
Qiang Xue committed
26

Qiang Xue committed
27 28 29
echo "<?php\n";
?>

Alexander Makarov committed
30
namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;
Qiang Xue committed
31

Qiang Xue committed
32
use Yii;
Alexander Makarov committed
33
use <?= ltrim($generator->modelClass, '\\') ?>;
34
use <?= ltrim($generator->searchModelClass, '\\') ?><?php if (isset($searchModelAlias)):?> as <?= $searchModelAlias ?><?php endif ?>;
Alexander Makarov committed
35
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
36
use yii\web\NotFoundHttpException;
Qiang Xue committed
37
use yii\web\VerbFilter;
Qiang Xue committed
38

Qiang Xue committed
39
/**
Alexander Makarov committed
40
 * <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
Qiang Xue committed
41
 */
Alexander Makarov committed
42
class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
Qiang Xue committed
43
{
Qiang Xue committed
44 45
	public function behaviors()
	{
Alexander Makarov committed
46 47
		return [
			'verbs' => [
Qiang Xue committed
48
				'class' => VerbFilter::className(),
Alexander Makarov committed
49 50 51 52 53
				'actions' => [
					'delete' => ['post'],
				],
			],
		];
Qiang Xue committed
54 55
	}

Qiang Xue committed
56
	/**
Alexander Makarov committed
57
	 * Lists all <?= $modelClass ?> models.
Qiang Xue committed
58 59 60 61
	 * @return mixed
	 */
	public function actionIndex()
	{
62
		$searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>;
63
		$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
Qiang Xue committed
64

Alexander Makarov committed
65
		return $this->render('index', [
Qiang Xue committed
66
			'dataProvider' => $dataProvider,
Qiang Xue committed
67
			'searchModel' => $searchModel,
Alexander Makarov committed
68
		]);
Qiang Xue committed
69 70
	}

Qiang Xue committed
71
	/**
Alexander Makarov committed
72 73
	 * Displays a single <?= $modelClass ?> model.
	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
Qiang Xue committed
74
	 * @return mixed
Qiang Xue committed
75
	 */
Alexander Makarov committed
76
	public function actionView(<?= $actionParams ?>)
Qiang Xue committed
77
	{
Alexander Makarov committed
78
		return $this->render('view', [
Alexander Makarov committed
79
			'model' => $this->findModel(<?= $actionParams ?>),
Alexander Makarov committed
80
		]);
Qiang Xue committed
81 82 83
	}

	/**
Alexander Makarov committed
84
	 * Creates a new <?= $modelClass ?> model.
Qiang Xue committed
85
	 * If creation is successful, the browser will be redirected to the 'view' page.
Qiang Xue committed
86
	 * @return mixed
Qiang Xue committed
87 88 89
	 */
	public function actionCreate()
	{
Alexander Makarov committed
90
		$model = new <?= $modelClass ?>;
Qiang Xue committed
91 92

		if ($model->load($_POST) && $model->save()) {
Alexander Makarov committed
93
			return $this->redirect(['view', <?= $urlParams ?>]);
Qiang Xue committed
94
		} else {
Alexander Makarov committed
95
			return $this->render('create', [
Qiang Xue committed
96
				'model' => $model,
Alexander Makarov committed
97
			]);
Qiang Xue committed
98 99 100 101
		}
	}

	/**
Alexander Makarov committed
102
	 * Updates an existing <?= $modelClass ?> model.
Qiang Xue committed
103
	 * If update is successful, the browser will be redirected to the 'view' page.
Alexander Makarov committed
104
	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
Qiang Xue committed
105
	 * @return mixed
Qiang Xue committed
106
	 */
Alexander Makarov committed
107
	public function actionUpdate(<?= $actionParams ?>)
Qiang Xue committed
108
	{
Alexander Makarov committed
109
		$model = $this->findModel(<?= $actionParams ?>);
Qiang Xue committed
110 111

		if ($model->load($_POST) && $model->save()) {
Alexander Makarov committed
112
			return $this->redirect(['view', <?= $urlParams ?>]);
Qiang Xue committed
113
		} else {
Alexander Makarov committed
114
			return $this->render('update', [
Qiang Xue committed
115
				'model' => $model,
Alexander Makarov committed
116
			]);
Qiang Xue committed
117 118 119 120
		}
	}

	/**
Alexander Makarov committed
121
	 * Deletes an existing <?= $modelClass ?> model.
Qiang Xue committed
122
	 * If deletion is successful, the browser will be redirected to the 'index' page.
Alexander Makarov committed
123
	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
Qiang Xue committed
124
	 * @return mixed
Qiang Xue committed
125
	 */
Alexander Makarov committed
126
	public function actionDelete(<?= $actionParams ?>)
Qiang Xue committed
127
	{
Alexander Makarov committed
128
		$this->findModel(<?= $actionParams ?>)->delete();
Alexander Makarov committed
129
		return $this->redirect(['index']);
Qiang Xue committed
130 131 132
	}

	/**
Alexander Makarov committed
133
	 * Finds the <?= $modelClass ?> model based on its primary key value.
Qiang Xue committed
134
	 * If the model is not found, a 404 HTTP exception will be thrown.
Alexander Makarov committed
135 136
	 * <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
	 * @return <?= $modelClass ?> the loaded model
137
	 * @throws NotFoundHttpException if the model cannot be found
Qiang Xue committed
138
	 */
Alexander Makarov committed
139
	protected function findModel(<?= $actionParams ?>)
Qiang Xue committed
140
	{
Qiang Xue committed
141 142 143
<?php
if (count($pks) === 1) {
	$condition = '$id';
144 145
	// find() would return Query when $id === null
	$nullCheck = '$id !== null && ';
Qiang Xue committed
146
} else {
Alexander Makarov committed
147
	$condition = [];
Qiang Xue committed
148 149
	foreach ($pks as $pk) {
		$condition[] = "'$pk' => \$$pk";
Qiang Xue committed
150
	}
Alexander Makarov committed
151
	$condition = '[' . implode(', ', $condition) . ']';
152
	$nullCheck = '';
Qiang Xue committed
153 154
}
?>
155
		if (<?= $nullCheck ?>($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) {
Qiang Xue committed
156 157
			return $model;
		} else {
158
			throw new NotFoundHttpException('The requested page does not exist.');
Qiang Xue committed
159 160 161
		}
	}
}