BaseController.php
3.92 KB
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
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\components\filters\AccessControl;
/**
* Site controller
*/
class BaseController extends Controller
{
public $layout = 'template';
//视图变量
protected $_actionParams = [];
//上一页
protected $_refer;
//用户实例
protected $user;
public function init()
{
//路径设置
Yii::setAlias('@web', Yii::getAlias('@admin'));
Yii::$app->setHomeUrl(Yii::getAlias('@web'));
Yii::$app->getRequest()->setBaseUrl(Yii::getAlias('@web'));
Yii::$app->getRequest()->setScriptUrl(Yii::getAlias('@web'));
//初始化用户实例
$this->user = Yii::$app->user->getIdentity();
//语言
Yii::$app->language = 'zh-CN';
parent::init();
}
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
],
];
}
public function beforeAction($action) {
$this->_refer = \common\lib\Referer::get();
$view = $this->getView();
$view->params['main_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][0] ? : '';
$view->params['sub_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][1][Yii::$app->controller->action->id][0] ? : '';
return parent::beforeAction($action);
}
/**
* 单个设置视图变量
* @param type $name
* @param type $value
* @return \backend\controllers\BaseController
*/
protected function setParam($name,$value) {
$this->_actionParams[$name] = $value;
return $this;
}
/**
* 批量设置视图变量
* @param array $params
* @return \backend\controllers\BaseController
*/
protected function setParams(array $params) {
$this->_actionParams = array_merge($this->_actionParams, $params);
return $this;
}
/**
* 跳转链接
* @param type $url
* @param type $statusCode
* @return type
*/
public function redirect($url, $statusCode = 302)
{
$url = (strpos($url,'http') === 0) ? $url : Yii::getAlias('@web/'.trim($url,'/'));
return Yii::$app->getResponse()->redirect($url, $statusCode);
}
/**
* 返回上一页
* @param type $defaultUrl
* @return type
*/
public function goBack($defaultUrl = null)
{
$url = Yii::$app->getUser()->getReturnUrl($defaultUrl);
$url = (strpos($url,'http') === 0) ? $url : Yii::getAlias('@web/'.trim($url,'/'));
return Yii::$app->getResponse()->redirect($url);
}
/**
* 刷新当前
* @param type $anchor
* @return type
*/
public function refresh($anchor = '')
{
$url = Yii::getAlias('@web/'.trim(Yii::$app->getRequest()->getUrl(),'/')) . $anchor;
return Yii::$app->getResponse()->redirect($url);
}
/**
* json返回
*/
public function renderJson($code,$message,$data = ''){
$retData = [
'code' => (int)$code,
'message' => $message,
'data' => $data,
'md5' => $data ? md5(json_encode($data)):''
];
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
Yii::$app->response->content = json_encode($retData);
Yii::$app->response->send();
Yii::$app->end();
}
/**
* 视图输出方法重载
* @param type $view
* @param type $params
* @return type
*/
public function render($view, $params = array()) {
$params = array_merge($this->_actionParams, [
'_refer'=>$this->_refer
], $params);
return parent::render($view, $params);
}
public function afterAction($action, $result) {
return parent::afterAction($action, $result);
}
}