-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.php
156 lines (137 loc) · 3.51 KB
/
Controller.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Created by PhpStorm.
* User: andkon
* Date: 06.06.14
* Time: 16:50
*/
namespace andkon\yii2actions;
use yii\base\Application;
use yii\base\Exception;
use yii\web\NotFoundHttpException;
class Controller extends \yii\web\Controller
{
/** @var null|string */
public $model = null;
/**
* Инициализация
*
* @return void
*/
public function init()
{
parent::init();
\Yii::$app->params['menu'] = [];
}
/**
* Возвращает имя модели
*
* @param bool|string $model_name
*
* @return bool
*/
protected function model($model_name = false)
{
if ($model_name) {
$this->model = $model_name;
}
return $this->model;
}
/**
* Список действий по умолчанию
*
* @return array
*/
public function actions()
{
$actions = [
'create' => 'andkon\yii2actions\actions\Create',
'update' => 'andkon\yii2actions\actions\Update',
'index' => 'andkon\yii2actions\actions\Index',
'view' => 'andkon\yii2actions\actions\View',
'delete' => 'andkon\yii2actions\actions\Delete',
];
return array_merge(parent::actions(), $actions);
}
/**
* Триггер перед действием
*
* @param Action $action
*
* @return mixed
*/
public function beforeAction($action)
{
if (in_array($action->id, ['logout', 'delete'])) {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
/**
* Возвращает данные из $_POST
*
* @param null|string $name
* @param bool $returnIsNull
*
* @return bool
*/
public function getPost($name = null, $returnIsNull = false)
{
if ($name == null) {
$name = $this->model();
}
$post = \Yii::$app->getRequest()->post($name);
if (empty($post)) {
return $returnIsNull;
}
return $post;
}
/**
* Возвращает url для данного контроллера
*
* @param string $view
* @param array $param
*
* @return mixed
*/
public function createUrl($view, $param = array())
{
if ($this->module instanceof Application) {
$path = $this->id . '/' . $view;
} else {
$path = $this->module->id . '/' . $this->id . '/' . $view;
}
$param = array_merge([$path], $param);
$url = \Yii::$app->getUrlManager()->createUrl($param);
return $url;
}
/**
* Возвращает модель связанную с контроллером
*
* @return ActiveRecord
*/
public function getModelName()
{
if ($this->model == null) {
throw new Exception('В контроллере ' . get_called_class() . ' не задана модель ($model)');
}
return $this->model;
}
/**
* Возвращает модель по id
*
* @param $id
*
* @return ActiveRecord
* @throws \yii\web\NotFoundHttpException
*/
public function findModel($id)
{
$model = $this->getModelName();
if (($model = $model::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}