Zend Framework相关
不使用view
1$this->_helper->viewRenderer->setNoRender();
Ajax相关
1 class AjaxRes 2 { 3 const STATUS_SUCCESS = 0; 4 const STATUS_ERROR = 1; 5 private $_arrRes; 6 function __construct(){ 7 $this->_arrRes = array('status'=>self::STATUS_SUCCESS,'statusinfo'=>'操作成功'); 8 } 9 public function toJson(){ 10 return json_encode($this->_arrRes); 11 } 12 public function setStatusError(){ 13 $this->_arrRes['status'] = self::STATUS_ERROR; 14 } 15 public function setStatusSuccess(){ 16 $this->_arrRes['status'] = self::STATUS_SUCCESS; 17 } 18 public function setStatusInfo($strStatusInfo){ 19 $this->_arrRes['statusinfo'] = $strStatusInfo; 20 } 21 public function set($key, $val){ 22 $this->_arrRes[$key] = $val; 23 } 24 }
1 public function init() 2 { 3 $this->initView(); 4 $this->view->baseUrl = $this->_request->getModuleName(); 5 $this->view->baseController = $this->_request->getControllerName(); 6 } 7 /** 8 * 校验参数 9 * @throws Exception_CanBeShow 10 */ 11 abstract protected function _checkParams(); 12 13 14 protected function _noLayout() { 15 $this->_helper->layout()->disableLayout(); 16 } 17 18 protected function _noViewScript() { 19 $this->_helper->viewRenderer->setNoRender(); 20 } 21 22 protected function _changeLayout($newLayout) { 23 $this->_helper->layout()->setLayout($newLayout); 24 } 25 26 protected function _changeView($viewScript) { 27 $this->view->render($viewScript); 28 } 29 30 protected function _sendJsonHeader() { 31 $this->_noLayout(); 32 $this->_noViewScript(); 33 header('Content-type: text/json; charset=utf-8'); 34 } 35 36 protected function _sendDownloadHeader($strFileName) { 37 $this->_noLayout(); 38 $this->_noViewScript(); 39 header('Content-Type: application/octet-stream'); 40 Header("Accept-Ranges: bytes"); 41 Header("Accept-Length: ".filesize($strFileName)); 42 $encoded_filename = urlencode($strFileName); 43 $encoded_filename = str_replace("+", "%20",$strFileName); 44 if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT'])) 45 { 46 header('Content-Disposition: attachment;filename="' . $encoded_filename . '"'); 47 } 48 else if (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) 49 { 50 header('Content-Disposition: attachment; filename*="utf8\'\'' . $strFileName . '"'); 51 } else { 52 header('Content-Disposition: attachment; filename="' . $strFileName . '"'); 53 } 54 }
使用方法:
1 $ajaxRes = new StoneSun_AjaxRes(); 2 $this->_sendJsonHeader(); 3 $ajaxRes->setStatusError(); 4 $ajaxRes->setStatusInfo("验证码不正确"); 5 echo $ajaxRes->toJson(); die(); 6 $ajaxRes->setStatusInfo('注册成功'); 7 echo $ajaxRes->toJson(); die();