forked from liupdhc/yii2-liuxy-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebController.php
360 lines (326 loc) · 9.43 KB
/
WebController.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* author: liupeng
* createTime: 2015/6/22 23:24
* description: ${TODO}
* file: WebController.php
*/
namespace yii\liuxy;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
class WebController extends \yii\web\Controller{
public $enableCsrfValidation = false;
/**
* 不需要验证用户登陆的的公开的actions
*
* @var array
*/
protected $publicActions = ['*'];
/**
* 请求类实例
*
* @var \yii\web\Request
*/
protected $request = null;
/**
* 响应对象
*
* @var \yii\web\Response
*/
protected $response = null;
/**
* 模板视图
* @var string
*/
protected $template;
/**
* 返回的状态码
*
* @var int
*/
protected $httpStatus = 200;
/**
* 存储要返回给用户的数据
*
* @var array
*/
protected $responseData = [
'code' => 200,
'msg' => 'success'
];
/**
* 已响应
* @var boolean
*/
protected $responded = false;
/**
* 会话用户
* @var mixed
*/
protected $user = null;
/**
* 请求响应格式
* 根据路由,解析出响应格式
* 再由响应格式输出相应的内容给用户
* 如
* 讲 /user/1001 或者 /user/1001.html指向 /user/entry
* 并根据用户id -> 1001获取相应用户信息返回给请求用户
* 如果是 /user/1001.json 则返回相应的json格式数据
*
* @var string
*
*/
protected $format = 'html';
/**
* 可用的格式
*
* @var array
*/
protected $formats = ['*'];
/**
* 判断当前页面是不是公开的不需要验证用户信息
* @return bool
*/
protected function isPublic() {
return (in_array($this->action->id, $this->publicActions) || in_array('*', $this->publicActions));
}
/**
* @inheritdoc
*/
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction'
]
];
}
/**
* 重写init函数,支持语言包设置
*/
public function init() {
if (isset($_GET['nginx_uri'])) {
unset($_GET['nginx_uri']);
}
parent::init();
$this->request = Yii::$app->request;
$this->response = Yii::$app->response;
$this->request->enableCsrfValidation = $this->enableCsrfValidation;
if (isset($_REQUEST['lang']) && $_REQUEST['lang'] != "") {
Yii::$app->language = $_REQUEST['lang'];
setcookie('lang', $_REQUEST['lang']);
} elseif (isset($_COOKIE['lang']) && $_COOKIE['lang'] != "") {
Yii::$app->language = $_COOKIE['lang'];
} else if (isset($_SERVER ['HTTP_ACCEPT_LANGUAGE'])) {
$lang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
Yii::$app->language = $lang[0];
}
}
protected function getRequest() {
if ($this->request === null) {
$this->request = Yii::$app->request;
}
return $this->request;
}
protected function getResponse() {
if ($this->response === null) {
$this->response = Yii::$app->response;
}
return $this->response;
}
/**
* 获取响应的格式
* @return string
*/
protected function getFormat() {
if ('' != ($extension = pathinfo($this->request->getPathInfo(), PATHINFO_EXTENSION))) {
$this->format = strtolower($extension);
}
return $this->format == '' ? 'html' : $this->format;
}
/**
* 设置响应格式
* @param $format
* @return \yii\liuxy\WebController
*/
public function setFormat($format) {
$this->format = $format == '' ? 'html' : strtolower(trim($format));
return $this;
}
/**
* 获取视图名称
* @return string
*/
public function getTemplate() {
if ($this->template == '')
$this->template = strtolower($this->action->id);
return $this->template;
}
/**
* 设置视图名称
* @param string $template
* @return \yii\liuxy\WebController
*/
public function setTemplate($template) {
$this->template = $template;
return $this;
}
/**
* 设置响应数据
* @param $name
* @param null $data
* @return $this
*/
public function setResponseData($name, $data = null) {
if ($name === null) {
$this->responseData = [];
} else if (is_array($name)) {
foreach ($name as $key => $val) {
$this->setResponseData($key, $val);
}
} elseif (is_scalar($name)) {
if ($data === null && isset($this->responseData[$name])) {
unset($this->responseData[$name]);
} else {
$this->responseData[$name] = $data;
}
}
return $this;
}
/**
* 根据响应格式返回数据
* @throws \Exception
*/
protected function respondByFormat() {
if ($this->responded) {
return false;
}
$this->responded = true;
$format = $this->getFormat();
$method = 'respondBy' . ucfirst($format) . 'Format';
$valid = ((in_array('*', $this->formats) || in_array($format, $this->formats)) && method_exists($this, $method));
if ($valid === false)
throw new \Exception('Invalid request fromat : ' . $format);
call_user_func(array($this, $method));
}
/**
* 输出html
*/
protected function respondByHtmlFormat() {
headers_sent() or header('Content-Type: text/html; charset=utf-8');
$response = $this->render($this->getTemplate(), $this->responseData);
echo $response;
$this->end();
}
/**
* 获取PHP的json配置项
* @return int
*/
protected function getJsonEncodeOptions() {
$jsonEncodeOptions = $this->request->get('_pretty') == 'true' ? (JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : (JSON_UNESCAPED_UNICODE);
return $jsonEncodeOptions;
}
/**
* 输出json
*/
protected function respondByJsonFormat() {
headers_sent() or header('Content-Type: application/json; charset=utf-8');
$response = json_encode($this->responseData, $this->getJsonEncodeOptions());
if ('' != ($callback = $this->request->get('callback'))) {
$response = $callback . '(' . $response . ');';
}
echo $response;
$this->end();
}
/**
* 从请求中获取值
* @param $key
* @param string $value
* @return string
*/
protected function get($key, $value = '') {
$params = ArrayHelper::merge($_GET, $_POST);
$params = ArrayHelper::merge($params, $this->request->getBodyParams());
if (isset($params[$key])) {
return $params[$key];
}
return $value;
}
/**
* 结束响应
* @throws \yii\base\ExitException
*/
public function end() {
ob_flush();
ob_clean();
/**
* 关闭非持久化的数据库连接
*/
$dbConfig = require(Yii::getAlias('@common').'/config/db.php');
$keys = array_keys($dbConfig);
if ($keys) {
foreach($keys as $item) {
$dbObject = \Yii::$app->get($item);
if ($dbObject instanceof \yii\db\Connection) {
if (!isset($dbObject->attributes[\PDO::ATTR_PERSISTENT])) {
if ($dbObject->getIsActive()) {
$dbObject->close();
}
}
}
}
}
Yii::$app->end();
}
/**
* 根据请求路径自动设置响应格式
* @param string $id
* @param array $params
* @return mixed|void
* @throws \yii\base\InvalidRouteException
*/
public function runAction($id, $params = []) {
if (strpos($id, '.') !== false) {
$parts = pathinfo($id);
$id = $parts['filename'];
$this->setFormat($parts['extension']);
}
parent::runAction($id, $params);
}
public function afterAction($action, $result) {
$errorHandler = Yii::$app->get('errorHandler');
if (!empty($errorHandler->exception)) {
$this->handleException($errorHandler->exception);
}
$result = parent::afterAction($action, $result);
if ($this->response->getStatusCode() == 200) {
return $this->respondByFormat();
}
return $result;
}
/**
* 处理异常信息
* @param $e
*/
public function handleException($e) {
$this->setResponseData(['code' => $e->getCode(), 'msg' => $e->getMessage(),'exception'=>YII_DEBUG ? VarDumper::dumpAsString($e): '']);
}
/**
* 兼容单页面获取变量
* @param string $view
* @param array $params
* @return mixed
*/
public function render($view, $params = []) {
$output = $this->getView ()->render ( $view, $params, $this );
$layoutFile = $this->findLayoutFile ( $this->getView () );
if ($layoutFile !== false) {
$ret = ArrayHelper::merge ( $params, [
'content' => $output
] );
return $this->getView ()->renderFile ( $layoutFile, $ret, $this );
} else {
return $output;
}
}
}