This commit is contained in:
ThinkPHP
2013-04-19 11:50:37 +08:00
2 changed files with 103 additions and 91 deletions

View File

@@ -10,6 +10,7 @@
// +----------------------------------------------------------------------
namespace Think;
use Think\View;
class Controller {
// 视图类实例
@@ -27,6 +28,7 @@ class Controller {
];
$this->view = new View();
$this->view->engine('think', $config);
//控制器初始化
if(method_exists($this, '_initialize'))
$this->_initialize();
@@ -37,11 +39,11 @@ class Controller {
* @access public
* @param string $template 模板文件名
* @param array $vars 模板输出变量
* @param string $cacheId 模板缓存标识
* @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);
}
/**
@@ -90,13 +92,23 @@ 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' :
case 'SCRIPT':
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
header('Content-Type:application/javascript; charset=utf-8');
exit($data);
case 'HTML':
// 返回html片段
header('Content-Type:text/html; charset=utf-8');
echo $data;
exit;
case 'TEXT':
// 返回一段纯文本
header('Content-Type:text/plain; charset=utf-8');
echo $data;
exit;
default:
// 用于扩展其他返回格式数据
Tag::listen('ajax_return', $data);
@@ -131,11 +143,11 @@ class Controller {
* 默认跳转操作 支持错误导向和正确跳转
* 调用模板显示 默认为public目录下面的success页面
* 提示页面为可配置 支持模板标签
* @access private
* @param string $message 提示信息
* @param Boolean $status 状态
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @access private
* @return void
*/
private function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false) {
@@ -165,7 +177,7 @@ class Controller {
//发生错误时候默认停留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 ;

View File

@@ -33,8 +33,8 @@ class View {
/**
* 模板变量赋值
* @access public
* @param mixed $name
* @param mixed $value
* @param mixed $name 变量名
* @param mixed $value 变量值
*/
public function assign($name, $value = ''){
if(is_array($name)) {
@@ -66,7 +66,7 @@ class View {
* @param array $config 引擎参数
* @return View
*/
public function engine($engine,$config=[]){
public function engine($engine, array $config = []){
$class = '\\Think\\View\\Driver\\' . ucwords($engine);
$this->engine = new $class($config);
return $this;
@@ -96,13 +96,13 @@ class View {
* @access public
* @param string $template 模板文件名
* @param array $vars 模板输出变量
* @param string $cacheId 模板缓存标识
* @param string $cache_id 模板缓存标识
* @return mixed
*/
public function display($template='',$vars=[],$cacheId='') {
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);
// 输出模板内容
@@ -118,10 +118,10 @@ class View {
* @access protected
* @param string $template 模板文件名或者内容
* @param array $vars 模板输出变量
* @param string $cacheId 模板缓存标识
* @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);
// 模板不存在 抛出异常
@@ -133,7 +133,7 @@ class View {
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);