ItemController::actionView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace zacksleo\yii2\cms\controllers;
4
5
use Yii;
6
use zacksleo\yii2\cms\models\Item;
7
use yii\data\ActiveDataProvider;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\filters\VerbFilter;
11
12
/**
13
 * ItemController implements the CRUD actions for Item model.
14
 */
15
class ItemController extends Controller
16
{
17
    /**
18
     * @inheritdoc
19
     */
20
    public function behaviors()
21
    {
22
        return [
23
            'verbs' => [
24
                'class' => VerbFilter::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
25
                'actions' => [
26
                    'delete' => ['POST'],
27
                ],
28
            ],
29
        ];
30
    }
31
32
    public function actions()
33
    {
34
        return [
35
            'upload' => [
36
                'class' => 'zacksleo\yii2\cms\actions\UEditorAction',
37
                'config' => [
38
                    //"imageUrlPrefix" => "http://www.baidu.com",//图片访问路径前缀
39
                    "imagePathFormat" => "/uploads/{yyyy}{mm}{dd}/{time}{rand:6}",
40
                    //上传保存路径
41
                    "imageRoot" => Yii::getAlias("@frontend/web"),
42
                ],
43
            ]
44
        ];
45
    }
46
47
    /**
48
     * Lists all Item models.
49
     * @return mixed
50
     */
51
    public function actionIndex()
52
    {
53
        $dataProvider = new ActiveDataProvider([
54
            'query' => Item::find(),
55
        ]);
56
57
        return $this->render('index', [
58
            'dataProvider' => $dataProvider,
59
        ]);
60
    }
61
62
    /**
63
     * Displays a single Item model.
64
     * @param integer $id
65
     * @return mixed
66
     */
67
    public function actionView($id)
68
    {
69
        return $this->render('view', [
70
            'model' => $this->findModel($id),
71
        ]);
72
    }
73
74
    /**
75
     * Creates a new Item model.
76
     * If creation is successful, the browser will be redirected to the 'view' page.
77
     * @return mixed
78
     */
79
    public function actionCreate()
80
    {
81
        $model = new Item();
82
83
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
84
            return $this->redirect(['view', 'id' => $model->id]);
85
        } else {
86
            return $this->render('create', [
87
                'model' => $model,
88
            ]);
89
        }
90
    }
91
92
    /**
93
     * Updates an existing Item model.
94
     * If update is successful, the browser will be redirected to the 'view' page.
95
     * @param integer $id
96
     * @return mixed
97
     */
98
    public function actionUpdate($id)
99
    {
100
        $model = $this->findModel($id);
101
102
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
103
            return $this->redirect(['view', 'id' => $model->id]);
104
        } else {
105
            return $this->render('update', [
106
                'model' => $model,
107
            ]);
108
        }
109
    }
110
111
    /**
112
     * Deletes an existing Item model.
113
     * If deletion is successful, the browser will be redirected to the 'index' page.
114
     * @param integer $id
115
     * @return mixed
116
     */
117
    public function actionDelete($id)
118
    {
119
        $this->findModel($id)->delete();
120
121
        return $this->redirect(['index']);
122
    }
123
124
    /**
125
     * Finds the Item model based on its primary key value.
126
     * If the model is not found, a 404 HTTP exception will be thrown.
127
     * @param integer $id
128
     * @return Item the loaded model
129
     * @throws NotFoundHttpException if the model cannot be found
130
     */
131
    protected function findModel($id)
132
    {
133
        if (($model = Item::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \zacksleo\yii2\cms\models\Item::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 134 which is incompatible with the return type documented by zacksleo\yii2\cms\contro...emController::findModel of type zacksleo\yii2\cms\models\Item.
Loading history...
134
            return $model;
135
        } else {
136
            throw new NotFoundHttpException('The requested page does not exist.');
137
        }
138
    }
139
}
140