PHP Classes

File: class.smartajaxcontroller.php

Recommend this page to a friend!
  Classes of Kristo Vaher   Smart AJAX Controller   class.smartajaxcontroller.php   Download  
File: class.smartajaxcontroller.php
Role: Class source
Content type: text/plain
Description: PHP class used to generate compressed JSON output for jQuery
Class: Smart AJAX Controller
Generate AJAX request responses in JSON format
Author: By
Last change:
Date: 14 years ago
Size: 1,310 bytes
 

Contents

Class file image Download
<?php

//SmartAjaxController functions as output generator for AJAX response
class SmartAjaxController {

   
//here we store the array sent as response
   
private $returnArray=array();
   
   
//here we store all the input variables
   
private $inputVariables=array();
   
    public function
__construct(){
        if(
extension_loaded('zlib')){ob_start('ob_gzhandler');} else {ob_start();}
    }
   
   
//Here we parse the serialized input variable string to proper input variables
   
public function setInputVariables($data){
        if(!empty(
$data)){
           
parse_str($data,$this->inputVariables);
        }
    }
   
   
//Here we return an input variable for use
   
public function getVar($key){
        if(isset(
$this->inputVariables[$key])){
            return
$this->inputVariables[$key];
        } else {
            return
false;
        }
    }
   
   
//assigning a new key for returning
   
public function setKey($key,$value){
        if(
$key!=''){
           
$this->returnArray[$key]=$value;
        }
    }
   
   
// When class serves no further purpose we echo the response
   
public function output(){
       
header('Content-Type: text/html; charset=utf-8');
       
header('Content-Encoding: gzip');
        if(empty(
$this->returnArray)){
           
$this->setKey('error',1);
           
$this->setKey('message','Nothing to return!');
        }
        echo
json_encode($this->returnArray);
       
ob_end_flush();
    }
   
}
?>