controller.php 4.17 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

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

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

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

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

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

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

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

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

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

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

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