From 165cd0cea71ecea372542f8eec03ef81025f364e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Thu, 18 Apr 2013 21:24:45 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Controller.php | 111 ++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 55 deletions(-) diff --git a/Library/Think/Controller.php b/Library/Think/Controller.php index e8cc412b..9d5a4c0a 100644 --- a/Library/Think/Controller.php +++ b/Library/Think/Controller.php @@ -13,22 +13,23 @@ namespace Think; class Controller { // 视图类实例 - protected $view = null; + protected $view = null; - /** + /** * 架构函数 初始化视图类 并采用内置模板引擎 * @access public */ public function __construct(){ // 模板引擎参数 - $config = [ - 'tpl_path' => MODULE_PATH.'View/', - 'cache_path' => RUNTIME_PATH.'Cache/', + $config = [ + 'tpl_path' => MODULE_PATH . 'View/', + 'cache_path' => RUNTIME_PATH . 'Cache/', ]; - $this->view = new View(); - $this->view->engine('think',$config); + $this->view = new View(); + $this->view->engine('think', $config); + //控制器初始化 - if(method_exists($this,'_initialize')) + if(method_exists($this, '_initialize')) $this->_initialize(); } @@ -36,38 +37,38 @@ class Controller { * 加载模板和页面输出 可以返回输出内容 * @access public * @param string $template 模板文件名 - * @param array $vars 模板输出变量 - * @param string $cacheId 模板缓存标识 + * @param array $vars 模板输出变量 + * @param string $cache_id 模板缓存标识 * @return mixed */ - public function display($template='',$vars=[],$cacheId=''){ - $this->view->display($template,$vars,$cacheId); + public function display($template = '', $vars = [], $cache_id = ''){ + $this->view->display($template, $vars, $cache_id); } /** * 渲染内容输出 * @access public * @param string $content 内容 - * @param array $vars 模板输出变量 + * @param array $vars 模板输出变量 * @return mixed */ - public function show($content,$vars=[]){ - $this->view->http('http_render_content',true)->display($content,$vars); + public function show($content, $vars = []){ + $this->view->http('http_render_content', true)->display($content, $vars); } /** * 模板变量赋值 * @access protected - * @param mixed $name 要显示的模板变量 + * @param mixed $name 要显示的模板变量 * @param mixed $value 变量的值 * @return void */ - public function assign($name,$value=''){ - $this->view->assign($name,$value); + public function assign($name, $value = ''){ + $this->view->assign($name, $value); } - public function __set($name,$value){ - return $this->assign($name,$value); + public function __set($name, $value){ + return $this->assign($name, $value); } /** @@ -77,29 +78,29 @@ class Controller { * @param String $type AJAX返回数据格式 * @return void */ - protected function ajaxReturn($data,$type='') { - if(empty($type)) $type = C('default_ajax_return'); + protected function ajaxReturn($data, $type='') { + if(empty($type)) $type = C('default_ajax_return'); switch (strtoupper($type)){ - case 'JSON' : + case 'JSON': // 返回JSON数据格式到客户端 包含状态信息 header('Content-Type:application/json; charset=utf-8'); exit(json_encode($data)); - case 'XML' : + case 'XML': // 返回xml格式数据 header('Content-Type:text/xml; charset=utf-8'); exit(xml_encode($data)); case 'JSONP': // 返回JSON数据格式到客户端 包含状态信息 header('Content-Type:application/json; charset=utf-8'); - $handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler'); - exit($handler.'('.json_encode($data).');'); - case 'EVAL' : + $handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler'); + exit($handler . '(' . json_encode($data) . ');'); + case 'EVAL': // 返回可执行的js脚本 header('Content-Type:text/html; charset=utf-8'); - exit($data); - default : + exit($data); + default: // 用于扩展其他返回格式数据 - Tag::listen('ajax_return',$data); + Tag::listen('ajax_return', $data); } } @@ -108,11 +109,11 @@ class Controller { * @access protected * @param string $message 错误信息 * @param string $jumpUrl 页面跳转地址 - * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 + * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ - protected function error($message,$jumpUrl='',$ajax=false) { - $this->dispatchJump($message,0,$jumpUrl,$ajax); + protected function error($message, $jumpUrl = '', $ajax = false) { + $this->dispatchJump($message, 0, $jumpUrl, $ajax); } /** @@ -120,52 +121,52 @@ class Controller { * @access protected * @param string $message 提示信息 * @param string $jumpUrl 页面跳转地址 - * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 + * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ - protected function success($message,$jumpUrl='',$ajax=false) { - $this->dispatchJump($message,1,$jumpUrl,$ajax); + protected function success($message, $jumpUrl = '', $ajax = false) { + $this->dispatchJump($message, 1, $jumpUrl, $ajax); } /** * 默认跳转操作 支持错误导向和正确跳转 * 调用模板显示 默认为public目录下面的success页面 * 提示页面为可配置 支持模板标签 - * @param string $message 提示信息 - * @param Boolean $status 状态 - * @param string $jumpUrl 页面跳转地址 - * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @access private + * @param string $message 提示信息 + * @param Boolean $status 状态 + * @param string $jumpUrl 页面跳转地址 + * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ - private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) { + private function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false) { if(true === $ajax || IS_AJAX) {// AJAX提交 - $data = is_array($ajax)?$ajax:[]; - $data['info'] = $message; - $data['status'] = $status; - $data['url'] = $jumpUrl; + $data = is_array($ajax) ? $ajax : []; + $data['info'] = $message; + $data['status'] = $status; + $data['url'] = $jumpUrl; $this->ajaxReturn($data); } - if(is_int($ajax)) $this->view->assign('waitSecond',$ajax); - if(!empty($jumpUrl)) $this->view->assign('jumpUrl',$jumpUrl); + if(is_int($ajax)) $this->view->assign('waitSecond', $ajax); + if(!empty($jumpUrl)) $this->view->assign('jumpUrl', $jumpUrl); // 提示标题 - $this->view->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_')); - $this->view->assign('status',$status); // 状态 + $this->view->assign('msgTitle', $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_')); + $this->view->assign('status', $status); // 状态 //保证输出不受静态缓存影响 C('HTML_CACHE_ON',false); if($status) { //发送成功信息 - $this->view->assign('message',$message);// 提示信息 + $this->view->assign('message', $message);// 提示信息 // 成功操作后默认停留1秒 - $this->view->assign('waitSecond','1'); + $this->view->assign('waitSecond', '1'); // 默认操作成功自动返回操作前页面 - if(!$jumpUrl) $this->view->assign("jumpUrl",$_SERVER["HTTP_REFERER"]); + if(!$jumpUrl) $this->view->assign("jumpUrl", $_SERVER["HTTP_REFERER"]); $this->display(C('success_tmpl')); }else{ - $this->view->assign('error',$message);// 提示信息 + $this->view->assign('error', $message);// 提示信息 //发生错误时候默认停留3秒 - $this->view->assign('waitSecond','3'); + $this->view->assign('waitSecond', '3'); // 默认发生错误的话自动返回上页 - if(!$jumpUrl) $this->view->assign('jumpUrl',"javascript:history.back(-1);"); + if(!$jumpUrl) $this->view->assign('jumpUrl', 'javascript:history.back(-1);'); $this->display(C('error_tmpl')); // 中止执行 避免出错后继续执行 exit ; From ee2ae91d10b4b317888e29b07052f4b450531eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Thu, 18 Apr 2013 21:58:17 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E8=A7=84=E8=8C=83=20JSONP=20=E5=92=8C=20EV?= =?UTF-8?q?AL=20=E7=9A=84=20ContentType=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Think/Controller.php b/Library/Think/Controller.php index 9d5a4c0a..d86a3c4f 100644 --- a/Library/Think/Controller.php +++ b/Library/Think/Controller.php @@ -91,12 +91,12 @@ class Controller { exit(xml_encode($data)); case 'JSONP': // 返回JSON数据格式到客户端 包含状态信息 - header('Content-Type:application/json; charset=utf-8'); + header('Content-Type:application/javascript; charset=utf-8'); $handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler'); exit($handler . '(' . json_encode($data) . ');'); case 'EVAL': // 返回可执行的js脚本 - header('Content-Type:text/html; charset=utf-8'); + header('Content-Type:application/javascript; charset=utf-8'); exit($data); default: // 用于扩展其他返回格式数据 From d0babf1ef51f3f83aeb012793ef1c9d4dbddeb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Thu, 18 Apr 2013 22:03:17 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9ajaxReturn=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E8=BF=94=E5=9B=9E=E5=80=BC=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=20EVAL=20=E4=B8=BA=20SCRIPT=20=E4=BD=BF=E4=B9=8B=E4=B8=8E=20jq?= =?UTF-8?q?uery=20=E5=AF=B9=E5=BA=94=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20jquery=20=E8=BF=98=E6=94=AF=E6=8C=81=E7=9A=84HTML=E5=92=8CTE?= =?UTF-8?q?XT=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Controller.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Library/Think/Controller.php b/Library/Think/Controller.php index d86a3c4f..12f70210 100644 --- a/Library/Think/Controller.php +++ b/Library/Think/Controller.php @@ -94,10 +94,18 @@ class Controller { header('Content-Type:application/javascript; charset=utf-8'); $handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler'); exit($handler . '(' . json_encode($data) . ');'); - case 'EVAL': + case 'SCRIPT': // 返回可执行的js脚本 header('Content-Type:application/javascript; charset=utf-8'); exit($data); + case 'HTML': + // 返回可执行的js脚本 + header('Content-Type:text/html; charset=utf-8'); + exit($data); + case 'TEXT': + // 返回可执行的js脚本 + header('Content-Type:text/plain; charset=utf-8'); + exit($data); default: // 用于扩展其他返回格式数据 Tag::listen('ajax_return', $data); From 3ddb6363f4b0a56b4efaedb57a23235cb94722d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Thu, 18 Apr 2013 22:05:38 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Think/Controller.php b/Library/Think/Controller.php index 12f70210..8c5a9143 100644 --- a/Library/Think/Controller.php +++ b/Library/Think/Controller.php @@ -99,11 +99,11 @@ class Controller { header('Content-Type:application/javascript; charset=utf-8'); exit($data); case 'HTML': - // 返回可执行的js脚本 + // 返回html片段 header('Content-Type:text/html; charset=utf-8'); exit($data); case 'TEXT': - // 返回可执行的js脚本 + // 返回一段纯文本 header('Content-Type:text/plain; charset=utf-8'); exit($data); default: From b81c570b1604ba7e9de10b23f99a0b7f3d46673d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Thu, 18 Apr 2013 22:10:31 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E9=81=BF=E5=85=8D=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=95=B4=E5=9E=8B=E6=95=B0=E5=80=BC=E5=87=BA=E9=94=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Controller.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Library/Think/Controller.php b/Library/Think/Controller.php index 8c5a9143..dbcad441 100644 --- a/Library/Think/Controller.php +++ b/Library/Think/Controller.php @@ -101,11 +101,13 @@ class Controller { case 'HTML': // 返回html片段 header('Content-Type:text/html; charset=utf-8'); - exit($data); + echo $data; + exit; case 'TEXT': // 返回一段纯文本 header('Content-Type:text/plain; charset=utf-8'); - exit($data); + echo $data; + exit; default: // 用于扩展其他返回格式数据 Tag::listen('ajax_return', $data); From 19a99463c9392e895164eb54d650798f1269e615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Thu, 18 Apr 2013 22:20:08 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=BC=95=E5=85=A5=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/Think/Controller.php b/Library/Think/Controller.php index dbcad441..9660a509 100644 --- a/Library/Think/Controller.php +++ b/Library/Think/Controller.php @@ -10,6 +10,7 @@ // +---------------------------------------------------------------------- namespace Think; +use Think\View; class Controller { // 视图类实例 From e0f90d9386f01b29ca8d70ed6f0194aa4efa7f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Fri, 19 Apr 2013 09:31:21 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/View.php | 70 +++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/Library/Think/View.php b/Library/Think/View.php index 1c5391b1..7f8e16f1 100644 --- a/Library/Think/View.php +++ b/Library/Think/View.php @@ -33,12 +33,12 @@ class View { /** * 模板变量赋值 * @access public - * @param mixed $name - * @param mixed $value + * @param mixed $name 变量名 + * @param mixed $value 变量值 */ - public function assign($name,$value=''){ + public function assign($name, $value = ''){ if(is_array($name)) { - $this->data = array_merge($this->data,$name); + $this->data = array_merge($this->data, $name); return $this; }else { $this->data[$name] = $value; @@ -51,12 +51,12 @@ class View { * @param mixed $name * @param mixed $value */ - public function __set($name,$value=''){ + public function __set($name, $value = ''){ $this->config[$name] = $value; } - public function __construct(array $config=[]){ - $this->config = array_merge($this->config,$config); + public function __construct(array $config = []){ + $this->config = array_merge($this->config, $config); } /** @@ -66,8 +66,8 @@ class View { * @param array $config 引擎参数 * @return View */ - public function engine($engine,$config=[]){ - $class = '\\Think\\View\\Driver\\'.ucwords($engine); + public function engine($engine, array $config = []){ + $class = '\\Think\\View\\Driver\\' . ucwords($engine); $this->engine = new $class($config); return $this; } @@ -75,7 +75,7 @@ class View { /** * 设置当前输出的模板主题 * @access public - * @param mixed $theme 主题名称 + * @param mixed $theme 主题名称 * @return View */ public function theme($theme){ @@ -94,17 +94,17 @@ class View { /** * 加载模板和页面输出 可以返回输出内容 * @access public - * @param string $template 模板文件名 - * @param array $vars 模板输出变量 - * @param string $cacheId 模板缓存标识 + * @param string $template 模板文件名 + * @param array $vars 模板输出变量 + * @param string $cache_id 模板缓存标识 * @return mixed */ - public function display($template='',$vars=[],$cacheId='') { - Tag::listen('view_begin',$template); + public function display($template = '', $vars = [], $cache_id = '') { + Tag::listen('view_begin', $template); // 解析并获取模板内容 - $content = $this->fetch($template,$vars,$cacheId); + $content = $this->fetch($template, $vars, $cache_id); // 输出内容过滤 - Tag::listen('view_filter',$content); + Tag::listen('view_filter', $content); // 输出模板内容 if($this->config['http_output_content']) { $this->render($content); @@ -117,26 +117,26 @@ class View { * 解析和获取模板内容 用于输出 * @access protected * @param string $template 模板文件名或者内容 - * @param array $vars 模板输出变量 - * @param string $cacheId 模板缓存标识 + * @param array $vars 模板输出变量 + * @param string $cache_id 模板缓存标识 * @return string */ - protected function fetch($template,$vars=[],$cacheId='') { + protected function fetch($template, $vars = [], $cache_id='') { if(!$this->config['http_render_content']) { $template = $this->parseTemplate($template); // 模板不存在 抛出异常 if(!is_file($template)) - E('template file not exists:'.$template); + E('template file not exists:' . $template); } $vars = $vars ? $vars : $this->data; // 页面缓存 ob_start(); ob_implicit_flush(0); if($this->engine) { // 指定模板引擎 - $this->engine->fetch($template,$vars,$cacheId); + $this->engine->fetch($template, $vars, $cache_id); }else{ // 原生PHP解析 extract($vars, EXTR_OVERWRITE); - is_file($template)?include $template:eval('?>'.$template); + is_file($template) ? include $template : eval('?>' . $template); } // 获取并清空缓存 return ob_get_clean(); @@ -152,17 +152,17 @@ class View { if(is_file($template)) { return $template; } - $template = str_replace(':','/',$template); + $template = str_replace(':', '/', $template); // 获取当前主题名称 $theme = $this->getTemplateTheme(); // 分析模板文件规则 if('' == $template) { // 如果模板文件名为空 按照默认规则定位 - $template = CONTROLLER_NAME.'/'.ACTION_NAME; - }elseif(false === strpos($template,'/')){ - $template = CONTROLLER_NAME.'/'.$template; + $template = CONTROLLER_NAME . '/' . ACTION_NAME; + }elseif(false === strpos($template, '/')){ + $template = CONTROLLER_NAME . '/' . $template; } - return ($this->config['view_path'] ? $this->config['view_path'] : MODULE_PATH.'View/').$theme.$template.$this->config['view_suffix']; + return ($this->config['view_path'] ? $this->config['view_path'] : MODULE_PATH . 'View/').$theme.$template.$this->config['view_suffix']; } /** @@ -182,14 +182,14 @@ class View { }elseif(Cookie::get('think_theme')){ $theme = Cookie::get('think_theme'); } - if(!is_dir(MODULE_PATH.'View/'.$theme)) { + if(!is_dir(MODULE_PATH . 'View/' . $theme)) { $theme = $this->config['default_theme']; } - Cookie::set('think_theme',$theme,864000); + Cookie::set('think_theme', $theme, 864000); }else{ $theme = $this->config['default_theme']; } - return $theme.'/'; + return $theme . '/'; } return ''; } @@ -200,9 +200,9 @@ class View { * @param mixed $config * @param mixed $value */ - public function http($config=[],$value=''){ + public function http($config = [], $value = ''){ if(is_array($config)) { - $this->config = array_merge($this->config,$config); + $this->config = array_merge($this->config, $config); }else{ $this->config[$config] = $value; } @@ -219,8 +219,8 @@ class View { */ private function render($content){ // 网页字符编码 - header('Content-Type:'.$this->config['http_content_type'].'; charset='.$this->config['http_charset']); - header('Cache-control: '.$this->config['http_cache_control']); // 页面缓存控制 + header('Content-Type:' . $this->config['http_content_type'] . '; charset=' . $this->config['http_charset']); + header('Cache-control:' . $this->config['http_cache_control']); // 页面缓存控制 header('X-Powered-By:ThinkPHP'); // 输出模板文件 echo $content;