PDA

View Full Version : ExtDirect+restful+phpMVC Integration



xp743
3 Jun 2010, 8:03 PM
After playing some trial and error with ExtDirect than I try it to join Extdirect and Restful + MySQL CRUD Class in single page.
this is the code :


if(isset($HTTP_RAW_POST_DATA)){
header('Content-Type: text/javascript');
$data = json_decode($HTTP_RAW_POST_DATA);
$resp = doResponse($data);
echo json_encode($resp);

}else if(isset($_POST['extAction'])){ // form post
$isForm = true;
$isUpload = $_POST['extUpload'] == 'true';

$datas = new DirectAction();
$datas->action = $_POST['extAction'];
$datas->method = $_POST['extMethod'];
$datas->type = $_POST['extType'];
$datas->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null; // not set for upload
$datas->data = array($_POST, $_FILES);

$resp = doResponse($datas);
if($isUpload){
echo '<html><body><textarea>';
echo json_encode($resp);
echo '</textarea></body></html>';
}else{
echo json_encode($resp);
}
}else{
$request = new Request(array('restful' => false));
require('controllers/' . $request->controller . '.php');
$controller_name = ucfirst($request->controller);
//$controller_name = ucfirst($request->controller);
$controller = new $controller_name;
echo $controller->dispatch($request);
}

than foreach class in Restful required, I extend it on a class like :


abstract class ExtMVC_Response {
protected $params = '';
public $success, $data, $type, $message, $script, $errors, $tid, $trace;
function __construct($params = '') {
$this->success = isset($params["success"]) ? $params["success"] : false;
$this->type = isset($params["type"]) ? $params["type"] : 'data';
$this->message = isset($params["message"]) ? $params["message"] : '';
$this->data = isset($params["data"]) ? $params["data"] : array();
}

function to_json() {
if ($this->type == 'data')
return json_encode(array(
'success' => $this->success,
'message' => $this->message,
'data' => $this->data
));
if ($this->type == 'script')
return json_encode(array(
'type' => $this->type,
'message' => $this->message,
'script' => 'eval('.$this->script.');'
));
}
}

In index file or router page just add :


class Response extends ExtMVC_Response {

function __construct($params = '') {
$params = array();
return parent::__construct($params);
}
}