controller.php 3.89 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3 4

use yii\helpers\StringHelper;

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

Qiang Xue committed
12 13 14 15 16 17 18 19
$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);

$pks = $generator->getTableSchema()->primaryKey;
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();
Qiang Xue committed
20

Qiang Xue committed
21 22 23
echo "<?php\n";
?>

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

Alexander Makarov committed
26 27
use <?= ltrim($generator->modelClass, '\\') ?>;
use <?= ltrim($generator->searchModelClass, '\\') ?>;
Qiang Xue committed
28
use yii\data\ActiveDataProvider;
Alexander Makarov committed
29
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
Qiang Xue committed
30
use yii\web\HttpException;
Qiang Xue committed
31
use yii\web\VerbFilter;
Qiang Xue committed
32

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

Qiang Xue committed
50
	/**
Alexander Makarov committed
51
	 * Lists all <?= $modelClass ?> models.
Qiang Xue committed
52 53 54 55
	 * @return mixed
	 */
	public function actionIndex()
	{
Alexander Makarov committed
56
		$searchModel = new <?= $searchModelClass ?>;
Qiang Xue committed
57 58
		$dataProvider = $searchModel->search($_GET);

Alexander Makarov committed
59
		return $this->render('index', [
Qiang Xue committed
60
			'dataProvider' => $dataProvider,
Qiang Xue committed
61
			'searchModel' => $searchModel,
Alexander Makarov committed
62
		]);
Qiang Xue committed
63 64
	}

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

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

		if ($model->load($_POST) && $model->save()) {
Alexander Makarov committed
87
			return $this->redirect(['view', <?= $urlParams ?>]);
Qiang Xue committed
88
		} else {
Alexander Makarov committed
89
			return $this->render('create', [
Qiang Xue committed
90
				'model' => $model,
Alexander Makarov committed
91
			]);
Qiang Xue committed
92 93 94 95
		}
	}

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

		if ($model->load($_POST) && $model->save()) {
Alexander Makarov committed
106
			return $this->redirect(['view', <?= $urlParams ?>]);
Qiang Xue committed
107
		} else {
Alexander Makarov committed
108
			return $this->render('update', [
Qiang Xue committed
109
				'model' => $model,
Alexander Makarov committed
110
			]);
Qiang Xue committed
111 112 113 114
		}
	}

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

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