controller.php 5.16 KB
Newer Older
Qiang Xue committed
1
<?php
2 3 4
/**
 * This is the template for generating a CRUD controller class file.
 */
Qiang Xue committed
5

6
use yii\db\ActiveRecordInterface;
Qiang Xue committed
7 8
use yii\helpers\StringHelper;

9 10 11

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
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
if ($modelClass === $searchModelClass) {
17
    $searchModelAlias = $searchModelClass . 'Search';
18
}
Qiang Xue committed
19

20
/* @var $class ActiveRecordInterface */
21 22
$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
<?php if (!empty($generator->searchModelClass)): ?>
Qiang Xue committed
35
use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
36
<?php else: ?>
37
use yii\data\ActiveDataProvider;
38
<?php endif; ?>
Alexander Makarov committed
39
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
40
use yii\web\NotFoundHttpException;
41
use yii\filters\VerbFilter;
Qiang Xue committed
42

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

    /**
     * Lists all <?= $modelClass ?> models.
     * @return mixed
     */
    public function actionIndex()
    {
66
<?php if (!empty($generator->searchModelClass)): ?>
Dmitry Chernikov committed
67 68
        $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
69 70 71

        return $this->render('index', [
            'searchModel' => $searchModel,
72
            'dataProvider' => $dataProvider,
73
        ]);
74
<?php else: ?>
75 76 77 78 79 80 81
        $dataProvider = new ActiveDataProvider([
            'query' => <?= $modelClass ?>::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
82
<?php endif; ?>
83 84 85 86
    }

    /**
     * Displays a single <?= $modelClass ?> model.
87
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
     * @return mixed
     */
    public function actionView(<?= $actionParams ?>)
    {
        return $this->render('view', [
            'model' => $this->findModel(<?= $actionParams ?>),
        ]);
    }

    /**
     * Creates a new <?= $modelClass ?> model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
Dmitry Chernikov committed
104
        $model = new <?= $modelClass ?>();
105 106 107 108 109 110 111 112 113 114 115 116 117

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', <?= $urlParams ?>]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing <?= $modelClass ?> model.
     * If update is successful, the browser will be redirected to the 'view' page.
118
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
     * @return mixed
     */
    public function actionUpdate(<?= $actionParams ?>)
    {
        $model = $this->findModel(<?= $actionParams ?>);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', <?= $urlParams ?>]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing <?= $modelClass ?> model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
137
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
138 139 140 141 142 143 144 145 146 147 148 149
     * @return mixed
     */
    public function actionDelete(<?= $actionParams ?>)
    {
        $this->findModel(<?= $actionParams ?>)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the <?= $modelClass ?> model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
150
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
151 152 153 154 155
     * @return <?=                   $modelClass ?> the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel(<?= $actionParams ?>)
    {
Qiang Xue committed
156 157
<?php
if (count($pks) === 1) {
158
    $condition = '$id';
Qiang Xue committed
159
} else {
160 161 162 163 164
    $condition = [];
    foreach ($pks as $pk) {
        $condition[] = "'$pk' => \$$pk";
    }
    $condition = '[' . implode(', ', $condition) . ']';
Qiang Xue committed
165 166
}
?>
Alexander Makarov committed
167
        if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
168 169 170 171 172
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
Qiang Xue committed
173
}