mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-04 06:12:48 +08:00
app模式重构
This commit is contained in:
@@ -15,23 +15,29 @@ use org\Transform;
|
||||
|
||||
class Controller {
|
||||
// 视图类实例
|
||||
protected $view = null;
|
||||
protected $view = null;
|
||||
protected $config = [
|
||||
'before_action_list' = [],
|
||||
'success_tmpl' = '',
|
||||
'error_tmpl' = '',
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数 初始化视图类 并采用内置模板引擎
|
||||
* @access public
|
||||
*/
|
||||
public function __construct(){
|
||||
public function __construct($config=[]){
|
||||
// 模板引擎参数
|
||||
$this->view = new View();
|
||||
|
||||
$this->view = new View();
|
||||
$this->config(empty($config)? Config::get() : $config);
|
||||
|
||||
// 控制器初始化
|
||||
if(method_exists($this, '_initialize')){
|
||||
$this->_initialize();
|
||||
}
|
||||
// 前置操作方法
|
||||
// 支持 ['action1','action2'] 或者 ['action1'=>['only'=>'index'],'action2'=>'except'=>'login']
|
||||
$list = Config::get('before_action_list');
|
||||
$list = $this->config['before_action_list'];
|
||||
if($list){
|
||||
foreach($list as $method=>$options){
|
||||
is_numeric($method)?
|
||||
@@ -41,6 +47,23 @@ class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置控制器参数
|
||||
* @access public
|
||||
* @param array $config 视图参数
|
||||
* @return View
|
||||
*/
|
||||
public function config($config=[]){
|
||||
if(is_array($config)){
|
||||
foreach($this->config as $key=>$val){
|
||||
if(isset($config[$key])){
|
||||
$this->config[$key] = $config[$key];
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 前置操作
|
||||
* @access protected
|
||||
@@ -71,8 +94,8 @@ class Controller {
|
||||
* @param string $cache_id 模板缓存标识
|
||||
* @return mixed
|
||||
*/
|
||||
public function display($template = '', $vars = [], $cache_id = ''){
|
||||
$this->view->display($template, $vars, $cache_id);
|
||||
public function fetch($template = '', $vars = [], $cache_id = ''){
|
||||
return $this->view->fetch($template, $vars, $cache_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +106,7 @@ class Controller {
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($content, $vars = []){
|
||||
$this->view->http('http_render_content', true)->display($content, $vars);
|
||||
return $this->view->fetch($content, $vars,'',true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,27 +120,6 @@ class Controller {
|
||||
$this->view->assign($name, $value);
|
||||
}
|
||||
|
||||
public function __set($name, $value){
|
||||
return $this->assign($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回API数据到客户端
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param integer $code 返回的code
|
||||
* @param mixed $msg 提示信息
|
||||
* @param string $type 返回数据格式
|
||||
* @return void
|
||||
*/
|
||||
protected function result($data,$code=0,$msg='',$type='') {
|
||||
$result['code'] = $code;
|
||||
$result['msg'] = $msg;
|
||||
$result['time'] = NOW_TIME;
|
||||
$result['data'] = $data;
|
||||
App::returnData($result,$type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax方式返回数据到客户端
|
||||
* @access protected
|
||||
@@ -126,19 +128,7 @@ class Controller {
|
||||
* @return void
|
||||
*/
|
||||
protected function ajaxReturn($data, $type='') {
|
||||
App::returnData($data,$type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Action跳转(URL重定向) 支持指定模块和延时跳转
|
||||
* @access protected
|
||||
* @param string $url 跳转的URL表达式
|
||||
* @param array $params 其它URL参数
|
||||
* @return void
|
||||
*/
|
||||
protected function redirect($url,$params=[]) {
|
||||
$url = U($url,$params);
|
||||
header('Location: ' . $url);
|
||||
Response::returnData($data,$type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,7 +140,7 @@ class Controller {
|
||||
* @return void
|
||||
*/
|
||||
protected function error($message, $jumpUrl = '', $ajax = false) {
|
||||
$this->dispatchJump($message, 0, $jumpUrl, $ajax);
|
||||
return $this->dispatchJump($message, 0, $jumpUrl, $ajax);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,7 +152,7 @@ class Controller {
|
||||
* @return void
|
||||
*/
|
||||
protected function success($message, $jumpUrl = '', $ajax = false) {
|
||||
$this->dispatchJump($message, 1, $jumpUrl, $ajax);
|
||||
return $this->dispatchJump($message, 1, $jumpUrl, $ajax);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +194,7 @@ class Controller {
|
||||
// 默认操作成功自动返回操作前页面
|
||||
if(!$jumpUrl)
|
||||
$data["jumpUrl"] = $_SERVER["HTTP_REFERER"];
|
||||
$this->display(Config::get('success_tmpl'),$data);
|
||||
return $this->display(Config::get('success_tmpl'),$data);
|
||||
}else{
|
||||
$data['error'] = $message;// 提示信息
|
||||
//发生错误时候默认停留3秒
|
||||
@@ -212,9 +202,7 @@ class Controller {
|
||||
// 默认发生错误的话自动返回上页
|
||||
if(!$jumpUrl)
|
||||
$data['jumpUrl'] = 'javascript:history.back(-1);';
|
||||
$this->display(Config::get('error_tmpl'),$data);
|
||||
// 中止执行 避免出错后继续执行
|
||||
exit ;
|
||||
return $this->display(Config::get('error_tmpl'),$data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user