// +---------------------------------------------------------------------- // $Id$ /** +------------------------------------------------------------------------------ * ThinkPHP 精简模式Action控制器基类 +------------------------------------------------------------------------------ */ abstract class Action extends Think {//类定义开始 protected $tVar = array(); // 模板输出变量 /** +---------------------------------------------------------- * 架构函数 +---------------------------------------------------------- * @access public +---------------------------------------------------------- */ public function __construct() { //控制器初始化 if(method_exists($this,'_initialize')) { $this->_initialize(); } } /** +---------------------------------------------------------- * 魔术方法 有不存在的操作的时候执行 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $method 方法名 * @param array $parms 参数 +---------------------------------------------------------- * @return mixed +---------------------------------------------------------- */ public function __call($method,$parms) { if(strtolower($method) == strtolower(ACTION_NAME)) { // 如果定义了_empty操作 则调用 if(method_exists($this,'_empty')) { $this->_empty($method,$parms); }else { // 抛出异常 throw_exception(L('_ERROR_ACTION_').ACTION_NAME); } }else{ throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_')); } } /** +---------------------------------------------------------- * 模板变量赋值 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param mixed $name * @param mixed $value +---------------------------------------------------------- */ protected function assign($name,$value=''){ if(is_array($name)) { $this->tVar = array_merge($this->tVar,$name); }elseif(is_object($name)){ foreach($name as $key =>$val) $this->tVar[$key] = $val; }else { $this->tVar[$name] = $value; } } /** +---------------------------------------------------------- * 模板显示 * 只支持PHP模板 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param string $templateFile 指定要调用的模板文件 * 默认为空 由系统自动定位模板文件 * @param string $charset 输出编码 * @param string $contentType 输出类型 +---------------------------------------------------------- * @return void +---------------------------------------------------------- */ protected function display($templateFile='',$charset='',$contentType='text/html') { if(empty($charset)) $charset = C('DEFAULT_CHARSET'); // 网页字符编码 header("Content-Type:".$contentType."; charset=".$charset); header("Cache-control: private"); //支持页面回跳 //页面缓存 ob_start(); ob_implicit_flush(0); // 自动定位模板文件 $templateFile = $this->parseTemplateFile($templateFile); // 模板阵列变量分解成为独立变量 extract($this->tVar, EXTR_OVERWRITE); // 直接载入PHP模板 include $templateFile; // 获取并清空缓存 $content = ob_get_clean(); echo $content; } /** +---------------------------------------------------------- * 自动定位模板文件 +---------------------------------------------------------- * @access private +---------------------------------------------------------- * @param string $templateFile 文件名 +---------------------------------------------------------- * @return string +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ private function parseTemplateFile($templateFile) { // Lite模式没有模板主题的概念 if(''==$templateFile) { // 如果模板文件名为空 按照默认规则定位 $templateFile = TMPL_PATH.'/'.MODULE_NAME.'/'.ACTION_NAME.C('TMPL_TEMPLATE_SUFFIX'); }elseif(strpos($templateFile,':')){ // 引入其它模块的操作模板 $templateFile = TMPL_PATH.'/'.str_replace(':','/',$templateFile).C('TMPL_TEMPLATE_SUFFIX'); }elseif(!is_file($templateFile)) { // 引入当前模块的其它操作模板 $templateFile = TMPL_PATH.'/'.MODULE_NAME.'/'.$templateFile.C('TMPL_TEMPLATE_SUFFIX'); } if(!file_exists_case($templateFile)) throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']'); return $templateFile; } }//类定义结束 ?>