controller.php 3.99 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";
?>

Qiang Xue committed
24
namespace <?php echo StringHelper::dirname(ltrim($generator->controllerClass, '\\')); ?>;
Qiang Xue committed
25 26

use <?php echo ltrim($generator->modelClass, '\\'); ?>;
Qiang Xue committed
27
use <?php echo ltrim($generator->searchModelClass, '\\'); ?>;
Qiang Xue committed
28 29 30
use yii\data\ActiveDataProvider;
use <?php echo ltrim($generator->baseControllerClass, '\\'); ?>;
use yii\web\HttpException;
Qiang Xue committed
31

Qiang Xue committed
32 33 34 35 36
/**
 * <?php echo $controllerClass; ?> implements the CRUD actions for <?php echo $modelClass; ?> model.
 */
class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n"; ?>
{
Qiang Xue committed
37 38 39 40 41 42
	/**
	 * Lists all <?php echo $modelClass; ?> models.
	 * @return mixed
	 */
	public function actionIndex()
	{
Qiang Xue committed
43 44 45
		$searchModel = new <?php echo $searchModelClass; ?>;
		$dataProvider = $searchModel->search($_GET);

Qiang Xue committed
46 47
		return $this->render('index', array(
			'dataProvider' => $dataProvider,
Qiang Xue committed
48
			'searchModel' => $searchModel,
Qiang Xue committed
49 50 51
		));
	}

Qiang Xue committed
52
	/**
Qiang Xue committed
53
	 * Displays a single <?php echo $modelClass; ?> model.
Qiang Xue committed
54
	 * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
Qiang Xue committed
55
	 * @return mixed
Qiang Xue committed
56
	 */
Qiang Xue committed
57
	public function actionView(<?php echo $actionParams; ?>)
Qiang Xue committed
58
	{
Qiang Xue committed
59
		return $this->render('view', array(
Qiang Xue committed
60
			'model' => $this->findModel(<?php echo $actionParams; ?>),
Qiang Xue committed
61 62 63 64
		));
	}

	/**
Qiang Xue committed
65
	 * Creates a new <?php echo $modelClass; ?> model.
Qiang Xue committed
66
	 * If creation is successful, the browser will be redirected to the 'view' page.
Qiang Xue committed
67
	 * @return mixed
Qiang Xue committed
68 69 70
	 */
	public function actionCreate()
	{
Qiang Xue committed
71 72 73
		$model = new <?php echo $modelClass; ?>;

		if ($model->load($_POST) && $model->save()) {
Qiang Xue committed
74
			return $this->redirect(array('view', <?php echo $urlParams; ?>));
Qiang Xue committed
75 76 77 78
		} else {
			return $this->render('create', array(
				'model' => $model,
			));
Qiang Xue committed
79 80 81 82
		}
	}

	/**
Qiang Xue committed
83
	 * Updates an existing <?php echo $modelClass; ?> model.
Qiang Xue committed
84
	 * If update is successful, the browser will be redirected to the 'view' page.
Qiang Xue committed
85
	 * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
Qiang Xue committed
86
	 * @return mixed
Qiang Xue committed
87
	 */
Qiang Xue committed
88
	public function actionUpdate(<?php echo $actionParams; ?>)
Qiang Xue committed
89
	{
Qiang Xue committed
90
		$model = $this->findModel(<?php echo $actionParams; ?>);
Qiang Xue committed
91 92

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

	/**
Qiang Xue committed
102 103
	 * Deletes an existing <?php echo $modelClass; ?> model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
Qiang Xue committed
104
	 * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
Qiang Xue committed
105
	 * @return mixed
Qiang Xue committed
106
	 */
Qiang Xue committed
107
	public function actionDelete(<?php echo $actionParams; ?>)
Qiang Xue committed
108
	{
Qiang Xue committed
109
		$this->findModel(<?php echo $actionParams; ?>)->delete();
Qiang Xue committed
110
		return $this->redirect(array('index'));
Qiang Xue committed
111 112 113
	}

	/**
Qiang Xue committed
114 115
	 * Finds the <?php echo $modelClass; ?> model based on its primary key value.
	 * If the model is not found, a 404 HTTP exception will be thrown.
Qiang Xue committed
116
	 * <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?>
Qiang Xue committed
117 118
	 * @return <?php echo $modelClass; ?> the loaded model
	 * @throws HttpException if the model cannot be found
Qiang Xue committed
119
	 */
Qiang Xue committed
120
	protected function findModel(<?php echo $actionParams; ?>)
Qiang Xue committed
121
	{
Qiang Xue committed
122 123 124 125 126 127 128
<?php
if (count($pks) === 1) {
	$condition = '$id';
} else {
	$condition = array();
	foreach ($pks as $pk) {
		$condition[] = "'$pk' => \$$pk";
Qiang Xue committed
129
	}
Qiang Xue committed
130 131 132
	$condition = 'array(' . implode(', ', $condition) . ')';
}
?>
Qiang Xue committed
133 134 135
		if (($model = <?php echo $modelClass; ?>::find(<?php echo $condition; ?>)) !== null) {
			return $model;
		} else {
Qiang Xue committed
136
			throw new HttpException(404, 'The requested page does not exist.');
Qiang Xue committed
137 138 139
		}
	}
}