forked from yii2tech/admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrudController.php
123 lines (116 loc) · 3.61 KB
/
CrudController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
73
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\admin;
use Yii;
use yii\base\Model;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii2tech\admin\behaviors\ModelControlBehavior;
/**
* CrudController implements a common set of actions for supporting CRUD for ActiveRecord.
*
* The class of the ActiveRecord should be specified via [[modelClass]], which must implement [[\yii\db\ActiveRecordInterface]].
* By default, the following actions are supported:
*
* - `index`: list of models
* - `view`: the details of a model
* - `create`: create a new model
* - `update`: update an existing model
* - `delete`: delete an existing model
*
* You may disable some of these actions by overriding [[actions()]] and unsetting the corresponding actions.
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class CrudController extends Controller
{
/**
* @var string the model class name. This property must be set.
* The model class must implement [[ActiveRecordInterface]].
*/
public $modelClass;
/**
* @var string class name of the model which should be used as search model.
* If not set it will be composed using [[modelClass]].
*/
public $searchModelClass;
/**
* @var string the scenario used for updating a model.
* @see Model::scenarios()
*/
public $updateScenario = Model::SCENARIO_DEFAULT;
/**
* @var string the scenario used for creating a model.
* @see Model::scenarios()
*/
public $createScenario = Model::SCENARIO_DEFAULT;
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'model' => [
'class' => ModelControlBehavior::className(),
'modelClass' => $this->modelClass,
'searchModelClass' => $this->searchModelClass,
],
'access' => [
'class' => AccessControl::className(),
'rules' => $this->accessRules(),
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Returns the access rules for this controller.
* This is method is a shortcut, allowing quick adjustment of the [[AccessControl]] filter attached at [[behaviors()]].
* Be careful in case you override [[behaviors()]] method, since it may loose configuration provided by this method.
* @return array list of access rules. See [[AccessControl::rules]] for details about rule specification.
*/
public function accessRules()
{
return [
[
'allow' => true,
'roles' => ['@'],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'index' => [
'class' => 'yii2tech\admin\actions\Index',
],
'view' => [
'class' => 'yii2tech\admin\actions\View',
],
'create' => [
'class' => 'yii2tech\admin\actions\Create',
'scenario' => $this->createScenario,
],
'update' => [
'class' => 'yii2tech\admin\actions\Update',
'scenario' => $this->updateScenario,
],
'delete' => [
'class' => 'yii2tech\admin\actions\Delete',
],
];
}
}