Action.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP 精简模式Action控制器基类
  15. +------------------------------------------------------------------------------
  16. */
  17. abstract class Action extends Think
  18. {//类定义开始
  19. protected $tVar = array(); // 模板输出变量
  20. /**
  21. +----------------------------------------------------------
  22. * 架构函数
  23. +----------------------------------------------------------
  24. * @access public
  25. +----------------------------------------------------------
  26. */
  27. public function __construct()
  28. {
  29. //控制器初始化
  30. if(method_exists($this,'_initialize')) {
  31. $this->_initialize();
  32. }
  33. }
  34. /**
  35. +----------------------------------------------------------
  36. * 魔术方法 有不存在的操作的时候执行
  37. +----------------------------------------------------------
  38. * @access public
  39. +----------------------------------------------------------
  40. * @param string $method 方法名
  41. * @param array $parms 参数
  42. +----------------------------------------------------------
  43. * @return mixed
  44. +----------------------------------------------------------
  45. */
  46. public function __call($method,$parms) {
  47. if(strtolower($method) == strtolower(ACTION_NAME)) {
  48. // 如果定义了_empty操作 则调用
  49. if(method_exists($this,'_empty')) {
  50. $this->_empty($method,$parms);
  51. }else {
  52. // 抛出异常
  53. throw_exception(L('_ERROR_ACTION_').ACTION_NAME);
  54. }
  55. }else{
  56. throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  57. }
  58. }
  59. /**
  60. +----------------------------------------------------------
  61. * 模板变量赋值
  62. +----------------------------------------------------------
  63. * @access protected
  64. +----------------------------------------------------------
  65. * @param mixed $name
  66. * @param mixed $value
  67. +----------------------------------------------------------
  68. */
  69. protected function assign($name,$value=''){
  70. if(is_array($name)) {
  71. $this->tVar = array_merge($this->tVar,$name);
  72. }elseif(is_object($name)){
  73. foreach($name as $key =>$val)
  74. $this->tVar[$key] = $val;
  75. }else {
  76. $this->tVar[$name] = $value;
  77. }
  78. }
  79. /**
  80. +----------------------------------------------------------
  81. * 模板显示
  82. * 只支持PHP模板
  83. +----------------------------------------------------------
  84. * @access protected
  85. +----------------------------------------------------------
  86. * @param string $templateFile 指定要调用的模板文件
  87. * 默认为空 由系统自动定位模板文件
  88. * @param string $charset 输出编码
  89. * @param string $contentType 输出类型
  90. +----------------------------------------------------------
  91. * @return void
  92. +----------------------------------------------------------
  93. */
  94. protected function display($templateFile='',$charset='',$contentType='text/html')
  95. {
  96. if(empty($charset)) $charset = C('DEFAULT_CHARSET');
  97. // 网页字符编码
  98. header("Content-Type:".$contentType."; charset=".$charset);
  99. header("Cache-control: private"); //支持页面回跳
  100. //页面缓存
  101. ob_start();
  102. ob_implicit_flush(0);
  103. // 自动定位模板文件
  104. $templateFile = $this->parseTemplateFile($templateFile);
  105. // 模板阵列变量分解成为独立变量
  106. extract($this->tVar, EXTR_OVERWRITE);
  107. // 直接载入PHP模板
  108. include $templateFile;
  109. // 获取并清空缓存
  110. $content = ob_get_clean();
  111. echo $content;
  112. }
  113. /**
  114. +----------------------------------------------------------
  115. * 自动定位模板文件
  116. +----------------------------------------------------------
  117. * @access private
  118. +----------------------------------------------------------
  119. * @param string $templateFile 文件名
  120. +----------------------------------------------------------
  121. * @return string
  122. +----------------------------------------------------------
  123. * @throws ThinkExecption
  124. +----------------------------------------------------------
  125. */
  126. private function parseTemplateFile($templateFile) {
  127. // Lite模式没有模板主题的概念
  128. if(''==$templateFile) {
  129. // 如果模板文件名为空 按照默认规则定位
  130. $templateFile = TMPL_PATH.'/'.MODULE_NAME.'/'.ACTION_NAME.C('TMPL_TEMPLATE_SUFFIX');
  131. }elseif(strpos($templateFile,':')){
  132. // 引入其它模块的操作模板
  133. $templateFile = TMPL_PATH.'/'.str_replace(':','/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
  134. }elseif(!is_file($templateFile)) {
  135. // 引入当前模块的其它操作模板
  136. $templateFile = TMPL_PATH.'/'.MODULE_NAME.'/'.$templateFile.C('TMPL_TEMPLATE_SUFFIX');
  137. }
  138. if(!file_exists_case($templateFile))
  139. throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
  140. return $templateFile;
  141. }
  142. }//类定义结束
  143. ?>