孙民选个人论坛

 鎵惧洖瀵嗙爜
 立即注册
鏌ョ湅: 124|鍥炲: 0
鎵撳嵃 涓婁竴涓婚 涓嬩竴涓婚

thinkphp鎺у埗鍣ㄧ被controller.class.php

[澶嶅埗閾炬帴]

789

涓婚

1158

甯栧瓙

4197

绉垎

管理员

Rank: 9Rank: 9Rank: 9

绉垎
4197
璺宠浆鍒版寚瀹氭ゼ灞
楼主
鍙戣〃浜 2017-11-12 16:58:49 | 鍙湅璇ヤ綔鑰 鍥炲笘濂栧姳 |鍊掑簭娴忚 |闃呰妯″紡
婧愮爜
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Think;
  12. /**
  13. * ThinkPHP 鎺у埗鍣ㄥ熀绫 鎶借薄绫
  14. */
  15. abstract class Controller {

  16.     /**
  17.      * 瑙嗗浘瀹炰緥瀵硅薄
  18.      * @var view
  19.      * @access protected
  20.      */   
  21.     protected $view     =  null;

  22.     /**
  23.      * 鎺у埗鍣ㄥ弬鏁
  24.      * @var config
  25.      * @access protected
  26.      */      
  27.     protected $config   =   array();

  28.    /**
  29.      * 鏋舵瀯鍑芥暟 鍙栧緱妯℃澘瀵硅薄瀹炰緥
  30.      * @access public
  31.      */
  32.     public function __construct() {
  33.         Hook::listen('action_begin',$this->config);
  34.         //瀹炰緥鍖栬鍥剧被
  35.         $this->view     = Think::instance('Think\View');
  36.         //鎺у埗鍣ㄥ垵濮嬪寲
  37.         if(method_exists($this,'_initialize'))
  38.             $this->_initialize();
  39.     }

  40.     /**
  41.      * 妯℃澘鏄剧ず 璋冪敤鍐呯疆鐨勬ā鏉垮紩鎿庢樉绀烘柟娉曪紝
  42.      * @access protected
  43.      * @param string $templateFile 鎸囧畾瑕佽皟鐢ㄧ殑妯℃澘鏂囦欢
  44.      * 榛樿涓虹┖ 鐢辩郴缁熻嚜鍔ㄥ畾浣嶆ā鏉挎枃浠
  45.      * @param string $charset 杈撳嚭缂栫爜
  46.      * @param string $contentType 杈撳嚭绫诲瀷
  47.      * @param string $content 杈撳嚭鍐呭
  48.      * @param string $prefix 妯℃澘缂撳瓨鍓嶇紑
  49.      * @return void
  50.      */
  51.     protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  52.         $this->view->display($templateFile,$charset,$contentType,$content,$prefix);
  53.     }

  54.     /**
  55.      * 杈撳嚭鍐呭鏂囨湰鍙互鍖呮嫭Html 骞舵敮鎸佸唴瀹硅В鏋
  56.      * @access protected
  57.      * @param string $content 杈撳嚭鍐呭
  58.      * @param string $charset 妯℃澘杈撳嚭瀛楃闆
  59.      * @param string $contentType 杈撳嚭绫诲瀷
  60.      * @param string $prefix 妯℃澘缂撳瓨鍓嶇紑
  61.      * @return mixed
  62.      */
  63.     protected function show($content,$charset='',$contentType='',$prefix='') {
  64.         $this->view->display('',$charset,$contentType,$content,$prefix);
  65.     }

  66.     /**
  67.      *  鑾峰彇杈撳嚭椤甸潰鍐呭
  68.      * 璋冪敤鍐呯疆鐨勬ā鏉垮紩鎿巉etch鏂规硶锛
  69.      * @access protected
  70.      * @param string $templateFile 鎸囧畾瑕佽皟鐢ㄧ殑妯℃澘鏂囦欢
  71.      * 榛樿涓虹┖ 鐢辩郴缁熻嚜鍔ㄥ畾浣嶆ā鏉挎枃浠
  72.      * @param string $content 妯℃澘杈撳嚭鍐呭
  73.      * @param string $prefix 妯℃澘缂撳瓨鍓嶇紑*
  74.      * @return string
  75.      */
  76.     protected function fetch($templateFile='',$content='',$prefix='') {
  77.         return $this->view->fetch($templateFile,$content,$prefix);
  78.     }

  79.     /**
  80.      *  鍒涘缓闈欐侀〉闈
  81.      * @access protected
  82.      * @htmlfile 鐢熸垚鐨勯潤鎬佹枃浠跺悕绉
  83.      * @htmlpath 鐢熸垚鐨勯潤鎬佹枃浠惰矾寰
  84.      * @param string $templateFile 鎸囧畾瑕佽皟鐢ㄧ殑妯℃澘鏂囦欢
  85.      * 榛樿涓虹┖ 鐢辩郴缁熻嚜鍔ㄥ畾浣嶆ā鏉挎枃浠
  86.      * @return string
  87.      */
  88.     protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
  89.         $content    =   $this->fetch($templateFile);
  90.         $htmlpath   =   !empty($htmlpath)?$htmlpath:HTML_PATH;
  91.         $htmlfile   =   $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  92.         Storage::put($htmlfile,$content,'html');
  93.         return $content;
  94.     }

  95.     /**
  96.      * 妯℃澘涓婚璁剧疆
  97.      * @access protected
  98.      * @param string $theme 妯$増涓婚
  99.      * @return Action
  100.      */
  101.     protected function theme($theme){
  102.         $this->view->theme($theme);
  103.         return $this;
  104.     }

  105.     /**
  106.      * 妯℃澘鍙橀噺璧嬪
  107.      * @access protected
  108.      * @param mixed $name 瑕佹樉绀虹殑妯℃澘鍙橀噺
  109.      * @param mixed $value 鍙橀噺鐨勫
  110.      * @return Action
  111.      */
  112.     protected function assign($name,$value='') {
  113.         $this->view->assign($name,$value);
  114.         return $this;
  115.     }

  116.     public function __set($name,$value) {
  117.         $this->assign($name,$value);
  118.     }

  119.     /**
  120.      * 鍙栧緱妯℃澘鏄剧ず鍙橀噺鐨勫
  121.      * @access protected
  122.      * @param string $name 妯℃澘鏄剧ず鍙橀噺
  123.      * @return mixed
  124.      */
  125.     public function get($name='') {
  126.         return $this->view->get($name);      
  127.     }

  128.     public function __get($name) {
  129.         return $this->get($name);
  130.     }

  131.     /**
  132.      * 妫娴嬫ā鏉垮彉閲忕殑鍊
  133.      * @access public
  134.      * @param string $name 鍚嶇О
  135.      * @return boolean
  136.      */
  137.     public function __isset($name) {
  138.         return $this->get($name);
  139.     }

  140.     /**
  141.      * 榄旀湳鏂规硶 鏈変笉瀛樺湪鐨勬搷浣滅殑鏃跺欐墽琛
  142.      * @access public
  143.      * @param string $method 鏂规硶鍚
  144.      * @param array $args 鍙傛暟
  145.      * @return mixed
  146.      */
  147.     public function __call($method,$args) {
  148.         if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
  149.             if(method_exists($this,'_empty')) {
  150.                 // 濡傛灉瀹氫箟浜哶empty鎿嶄綔 鍒欒皟鐢
  151.                 $this->_empty($method,$args);
  152.             }elseif(file_exists_case($this->view->parseTemplate())){
  153.                 // 妫鏌ユ槸鍚﹀瓨鍦ㄩ粯璁ゆā鐗 濡傛灉鏈夌洿鎺ヨ緭鍑烘ā鐗
  154.                 $this->display();
  155.             }else{
  156.                 E(L('_ERROR_ACTION_').':'.ACTION_NAME);
  157.             }
  158.         }else{
  159.             E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  160.             return;
  161.         }
  162.     }

  163.     /**
  164.      * 鎿嶄綔閿欒璺宠浆鐨勫揩鎹锋柟娉
  165.      * @access protected
  166.      * @param string $message 閿欒淇℃伅
  167.      * @param string $jumpUrl 椤甸潰璺宠浆鍦板潃
  168.      * @param mixed $ajax 鏄惁涓篈jax鏂瑰紡 褰撴暟瀛楁椂鎸囧畾璺宠浆鏃堕棿
  169.      * @return void
  170.      */
  171.     protected function error($message='',$jumpUrl='',$ajax=false) {
  172.         $this->dispatchJump($message,0,$jumpUrl,$ajax);
  173.     }

  174.     /**
  175.      * 鎿嶄綔鎴愬姛璺宠浆鐨勫揩鎹锋柟娉
  176.      * @access protected
  177.      * @param string $message 鎻愮ず淇℃伅
  178.      * @param string $jumpUrl 椤甸潰璺宠浆鍦板潃
  179.      * @param mixed $ajax 鏄惁涓篈jax鏂瑰紡 褰撴暟瀛楁椂鎸囧畾璺宠浆鏃堕棿
  180.      * @return void
  181.      */
  182.     protected function success($message='',$jumpUrl='',$ajax=false) {
  183.         $this->dispatchJump($message,1,$jumpUrl,$ajax);
  184.     }

  185.     /**
  186.      * Ajax鏂瑰紡杩斿洖鏁版嵁鍒板鎴风
  187.      * @access protected
  188.      * @param mixed $data 瑕佽繑鍥炵殑鏁版嵁
  189.      * @param String $type AJAX杩斿洖鏁版嵁鏍煎紡
  190.      * @param int $json_option 浼犻掔粰json_encode鐨刼ption鍙傛暟
  191.      * @return void
  192.      */
  193.     protected function ajaxReturn($data,$type='',$json_option=0) {
  194.         if(empty($type)) $type  =   C('DEFAULT_AJAX_RETURN');
  195.         switch (strtoupper($type)){
  196.             case 'JSON' :
  197.                 // 杩斿洖JSON鏁版嵁鏍煎紡鍒板鎴风 鍖呭惈鐘舵佷俊鎭
  198.                 header('Content-Type:application/json; charset=utf-8');
  199.                 exit(json_encode($data,$json_option));
  200.             case 'XML'  :
  201.                 // 杩斿洖xml鏍煎紡鏁版嵁
  202.                 header('Content-Type:text/xml; charset=utf-8');
  203.                 exit(xml_encode($data));
  204.             case 'JSONP':
  205.                 // 杩斿洖JSON鏁版嵁鏍煎紡鍒板鎴风 鍖呭惈鐘舵佷俊鎭
  206.                 header('Content-Type:application/json; charset=utf-8');
  207.                 $handler  =   isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  208.                 exit($handler.'('.json_encode($data,$json_option).');');  
  209.             case 'EVAL' :
  210.                 // 杩斿洖鍙墽琛岀殑js鑴氭湰
  211.                 header('Content-Type:text/html; charset=utf-8');
  212.                 exit($data);            
  213.             default     :
  214.                 // 鐢ㄤ簬鎵╁睍鍏朵粬杩斿洖鏍煎紡鏁版嵁
  215.                 Hook::listen('ajax_return',$data);
  216.         }
  217.     }

  218.     /**
  219.      * Action璺宠浆(URL閲嶅畾鍚戯級 鏀寔鎸囧畾妯″潡鍜屽欢鏃惰烦杞
  220.      * @access protected
  221.      * @param string $url 璺宠浆鐨刄RL琛ㄨ揪寮
  222.      * @param array $params 鍏跺畠URL鍙傛暟
  223.      * @param integer $delay 寤舵椂璺宠浆鐨勬椂闂 鍗曚綅涓虹
  224.      * @param string $msg 璺宠浆鎻愮ず淇℃伅
  225.      * @return void
  226.      */
  227.     protected function redirect($url,$params=array(),$delay=0,$msg='') {
  228.         $url    =   U($url,$params);
  229.         redirect($url,$delay,$msg);
  230.     }

  231.     /**
  232.      * 榛樿璺宠浆鎿嶄綔 鏀寔閿欒瀵煎悜鍜屾纭烦杞
  233.      * 璋冪敤妯℃澘鏄剧ず 榛樿涓簆ublic鐩綍涓嬮潰鐨剆uccess椤甸潰
  234.      * 鎻愮ず椤甸潰涓哄彲閰嶇疆 鏀寔妯℃澘鏍囩
  235.      * @param string $message 鎻愮ず淇℃伅
  236.      * @param Boolean $status 鐘舵
  237.      * @param string $jumpUrl 椤甸潰璺宠浆鍦板潃
  238.      * @param mixed $ajax 鏄惁涓篈jax鏂瑰紡 褰撴暟瀛楁椂鎸囧畾璺宠浆鏃堕棿
  239.      * @access private
  240.      * @return void
  241.      */
  242.     private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) {
  243.         if(true === $ajax || IS_AJAX) {// AJAX鎻愪氦
  244.             $data           =   is_array($ajax)?$ajax:array();
  245.             $data['info']   =   $message;
  246.             $data['status'] =   $status;
  247.             $data['url']    =   $jumpUrl;
  248.             $this->ajaxReturn($data);
  249.         }
  250.         if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  251.         if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  252.         // 鎻愮ず鏍囬
  253.         $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  254.         //濡傛灉璁剧疆浜嗗叧闂獥鍙o紝鍒欐彁绀哄畬姣曞悗鑷姩鍏抽棴绐楀彛
  255.         if($this->get('closeWin'))    $this->assign('jumpUrl','javascript:window.close();');
  256.         $this->assign('status',$status);   // 鐘舵
  257.         //淇濊瘉杈撳嚭涓嶅彈闈欐佺紦瀛樺奖鍝
  258.         C('HTML_CACHE_ON',false);
  259.         if($status) { //鍙戦佹垚鍔熶俊鎭
  260.             $this->assign('message',$message);// 鎻愮ず淇℃伅
  261.             // 鎴愬姛鎿嶄綔鍚庨粯璁ゅ仠鐣1绉
  262.             if(!isset($this->waitSecond))    $this->assign('waitSecond','1');
  263.             // 榛樿鎿嶄綔鎴愬姛鑷姩杩斿洖鎿嶄綔鍓嶉〉闈
  264.             if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  265.             $this->display(C('TMPL_ACTION_SUCCESS'));
  266.         }else{
  267.             $this->assign('error',$message);// 鎻愮ず淇℃伅
  268.             //鍙戠敓閿欒鏃跺欓粯璁ゅ仠鐣3绉
  269.             if(!isset($this->waitSecond))    $this->assign('waitSecond','3');
  270.             // 榛樿鍙戠敓閿欒鐨勮瘽鑷姩杩斿洖涓婇〉
  271.             if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  272.             $this->display(C('TMPL_ACTION_ERROR'));
  273.             // 涓鎵ц  閬垮厤鍑洪敊鍚庣户缁墽琛
  274.             exit ;
  275.         }
  276.     }

  277.    /**
  278.      * 鏋愭瀯鏂规硶
  279.      * @access public
  280.      */
  281.     public function __destruct() {
  282.         // 鎵ц鍚庣画鎿嶄綔
  283.         Hook::listen('action_end');
  284.     }
  285. }
  286. // 璁剧疆鎺у埗鍣ㄥ埆鍚 渚夸簬鍗囩骇
  287. class_alias('Think\Controller','Think\Action');
澶嶅埗浠g爜


鍥炲

浣跨敤閬撳叿 涓炬姤

鎮ㄩ渶瑕佺櫥褰曞悗鎵嶅彲浠ュ洖甯 鐧诲綍 | 立即注册

鏈増绉垎瑙勫垯

QQ|Archiver|手机版|小黑屋|孙民选个人论坛

GMT+8, 2026-5-2 04:39 , Processed in 0.075009 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

蹇熷洖澶 杩斿洖椤堕儴 杩斿洖鍒楄〃
0.0888s