public
Last active

JApi, short for JSON API, is an action handler for Yii.

  • Download Gist
JApi.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<?php
/**
* JApi, short of JSON API, is an action handler for Yii.
*
* JApi maps requests in the form ?r=controller/japu&action=method to controller->japiMethod. Then, anything returned from
* japiMethod is encoded in JSON and sent to the client. Parameters in the function are mapped to _GET varibles. JApi
* follows the requirments of the parameters, such as optional and required.
*
* EXAMPLE:
in ExampleController.php
<code>
<?php
class ExampleController extends Controller
{
* public function japiTest($required,$optional="abc")
* {
* return array('r'=>$required,'o'=>$optional);
* }
* public function actions()
* {
* return array(
* 'japi'=>'JApi',
* );
* }
* }
* ?>
</code>
*
* Request: /?r=example/japi&action=test&required=text
* Returns: {'r':'text','o':'abc'}
*
* Request: /?r=example/japi&action=test&required=text&optional=text2
* Returns: {'r':'text','o':'text2'}
*
* Request: /?r=example/japi&action=test
* Returns: 500 Internal Server Error
*
*
* @package Japi
* @author Kevin Gravier <kevin@mrkmg.com>
* @license MIT http://www.opensource.org/licenses/mit-license.php
* @version 1.0.0
* @todo Adjust errors to be more informational, for example change missing required params to 400 and missing actions to 404
*/
class JApi extends CAction
{
/**
* Holds the name of the requested action
*
* @access private
* @var string name of function
*/
private $func;
/**
* Hold the data returned by requested action
*
* @access private
* @var mixed data to be serialized and returned via json object
*/
private $returnData = NULL;
/**
* Runs the requested action
*
* Finds requested action in controller, runs function, and sends result to client via JSON. If no action is passed
* it assums the action is index
*
* @access public
* @throws CHttpException when requested action was not found in the controller
*/
public function run()
{
if(!isset($_GET['action']) or is_null($_GET['action']) or $_GET['action']=='')
{
$_GET['action'] = 'index';
}
$this->makeFunc($_GET['action']);
if(!method_exists($this->getController(),$this->func))
{
throw new CHttpException(404,'Action not found: '.$this->func);
}
$this->runFunc();
$this->send();
}
/**
* Runs the reuested action
*
* Uses PHP's ReflectionMethod to run the requested action with params
*
* @access private
* @throws CHttpException when required params are missing from the request
*/
private function runFunc()
{
$method = new ReflectionMethod($this->getController(),$this->func);
if($method->getNumberOfParameters()===0)
{
$this->returnData = $this->getController()->{$this->func}();
}
else
{
$ps=array();
$params = $_GET;
foreach($method->getParameters() as $i=>$param)
{
$name=$param->getName();
if(isset($params[$name]))
{
if($param->isArray())
$ps[]=is_array($params[$name]) ? $params[$name] : array($params[$name]);
else if(!is_array($params[$name]))
$ps[]=$params[$name];
else
throw new CHttpException(400,'Request Invalid');
}
else if($param->isDefaultValueAvailable())
$ps[]=$param->getDefaultValue();
else
throw new CHttpException(400,'Request Invalid');
}
$this->returnData = $method->invokeArgs($this->getController(),$ps);
}
}
/**
* Returns the function name needed to be run in the controller to respond to the request
*
* @access private
*/
private function makeFunc($action)
{
$this->func = 'japi'.ucfirst(strtolower($action));
}
/**
* Sends the result of the request to the client in JSON
*
* Sets header "Content-Type" to "application/json", encodes the result in json, then sends encoded result to client
*
* @access private
*/
private function send()
{
header('Content-Type: application/json');
echo CJavaScript::jsonEncode($this->returnData);
}
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.