controller.php 4.87 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
if ($modelClass === $searchModelClass) {
17
    $searchModelAlias = $searchModelClass . 'Search';
18
}
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

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

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

    /**
     * Lists all <?= $modelClass ?> models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>;
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    /**
     * Displays a single <?= $modelClass ?> model.
73
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
74 75 76 77 78 79 80 81 82 83 84 85 86 87 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()
    {
        $model = new <?= $modelClass ?>;

        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.
104
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
     * @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.
123
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
124 125 126 127 128 129 130 131 132 133 134 135
     * @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.
136
     * <?= implode("\n     * ", $actionParamComments) . "\n" ?>
137 138 139 140 141
     * @return <?=                   $modelClass ?> the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel(<?= $actionParams ?>)
    {
Qiang Xue committed
142 143
<?php
if (count($pks) === 1) {
144 145 146
    $condition = '$id';
    // find() would return Query when $id === null
    $nullCheck = '$id !== null && ';
Qiang Xue committed
147
} else {
148 149 150 151 152 153
    $condition = [];
    foreach ($pks as $pk) {
        $condition[] = "'$pk' => \$$pk";
    }
    $condition = '[' . implode(', ', $condition) . ']';
    $nullCheck = '';
Qiang Xue committed
154 155
}
?>
156 157 158 159 160 161
        if (<?= $nullCheck ?>($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
Qiang Xue committed
162
}