controller.php 4.07 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
 * This is the template for generating a CRUD controller class file.
 *
Alexander Makarov committed
8
 * @var yii\web\View $this
Qiang Xue committed
9
 * @var yii\gii\generators\crud\Generator $generator
Qiang Xue committed
10
 */
Qiang Xue committed
11

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

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

Qiang Xue committed
24 25 26
echo "<?php\n";
?>

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

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

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

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

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

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

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

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

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

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

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

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