768x90 Getting Online Shouldn't be Tough- $7.49 .com Domains at Go Daddy

Servicing Restful CakePHP in Flex/Air

I have found a great framework of Dima Berastau which is called Restfulx, this framework is helping you to abstract your application from repetitive CRUD code and switch/synchronize between various data providers with minimal effort, very suitable for Ruby on rails and Phyton(Django) Developers. What about PHP?

Honestly Restfulx is suitable for php developer like CakePHP, CodeIgniter or any MVC and Restful framework as their webservice in flex or Air. But now I would like to share about simple Restful webservice utility for php framework, my handmade, if i get wrong please send me feedback correction.

The main idea of using is :
//http://localhost/baking_cake/users/?first_name=yacobus

RestfulPhp.index(User,{first_name: "yacobus"}, onIndexSuccess, onIndexFailure);


//http://localhost/baking_cake/users
  var params:Object = new Object;
  params.first_name = "yacobus";
  params.last_name = "reinhart";
  params.current_company = "kiranatama.com";
  RestfulPhp.add(User,params, onCreateSuccess, onCreateFailure);



  //http://localhost/baking_cake/users/7.xml
  var params:Object = new Object;
  params.id = "7";
  params.first_name = "yacobus";
  params.last_name = "reinhart";
  params.current_company = "www.wgs.co.id";
  RestfulPhp.edit(User,params, onEditSuccess, onEditFailure);


  //http://localhost/baking_cake/users/7.xml
  RestfulPhp.show(User,{id: "7"}, onShowSuccess, onShowFailure);

  //http://localhost/baking_cake/users/7.xml
  RestfulPhp.delete(User,{id: "7"}, onDeleteSuccess, onDeleteFailure);

First step and the last are :

you should create utility class or any class name that will have function as webservice, for example : I create RestfulPhp in com.reinhartlab.share


package com.teapoci.share
{
  import flash.utils.getQualifiedClassName;
  import mx.messaging.messages.HTTPRequestMessage;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;
  import mx.rpc.http.HTTPService;

  public class RestfulPhp {

    private static var restful:RestfulPhp;
    private static var baseURL:String = "http://localhost/baking_cake/";
    public static function getInstance():RestfulPhp {
      (restful == null) ? restful = new RestfulPhp();
      return restful;
    }

    public function index(model:Class, params:Object = null, successFunction:Function = null, faultFunction: Function = null, resultFormat:String = "e4x"):void
    {
      var sendData:XML = ;
      var httpService : HTTPService = new HTTPService();
      httpService.contentType = "text/xml";
      httpService.method = HTTPRequestMessage.GET_METHOD;
      doHttpService(model, httpService, "GET", resultFormat, params, successFunction,  faultFunction);
     }

     public function add(model:Class, resultFormat:String = "e4x"):void{
       var sendData:XML = ;
       var httpService : HTTPService = new HTTPService();
       httpService.contentType = "text/xml";
       httpService.method = HTTPRequestMessage.POST_METHOD;
       doHttpService(model, httpService, "POST", resultFormat, params, successFunction, faultFunction);
     }

     public function edit(model:Class, params:Object = null, successFunction:Function = null, faultFunction: Function = null, resultFormat:String = "e4x"):void
     {
        var sendData:XML = ;
        var httpService : HTTPService = new HTTPService();
        httpService.contentType = "text/xml";
        httpService.method = HTTPRequestMessage.PUT_METHOD;
        doHttpService(model, httpService, "PUT", resultFormat, params, successFunction, faultFunction);
     }

     public function delete(model:Class, params:Object = null, successFunction:Function = null, faultFunction: Function = null, resultFormat:String = "e4x"):void
     {
         var httpService : HTTPService = new HTTPService();
         httpService.contentType = "text/xml";
         httpService.method = HTTPRequestMessage.DELETE_METHOD;
         doHttpService(model, httpService, "DELETE", resultFormat, params, successFunction, faultFunction);
     }

     public function show(model:Class, params:Object = null, successFunction:Function = null, faultFunction: Function = null, resultFormat:String = "e4x"):void
     {
         var httpService : HTTPService = new HTTPService();
         httpService.contentType = "text/xml";
         httpService.method = HTTPRequestMessage.GET_METHOD;
         doHttpService(model, httpService, "GET", resultFormat, params, successFunction, faultFunction);
     }

     private function doHttpService(_model:Class, _service:HTTPService, _method:String, _format:String, _params:Object,_onSuccess:Function, _onFailure:Function):void
     {         
         var sendData:XML = ;
         (_params!= null) ? sendData.appendChild(_params);
         var controllerUrl:String = flash.utils.getQualifiedClassName(_model)+"s";
         _service.url =  baseURL + controllerUrl;
        if(_method != "POST" && _params.id != null){
        _service.url = _service.url + "/" + String(_params.id)+".xml";
      }
             

     _service.headers = {"REQUEST_METHOD": _method,
                         "X_HTTP_METHOD_OVERRIDE": _method};
     _service.resultFormat = _format;
     _service.addEventListener( ResultEvent.RESULT, _onSuccess);

     if(_onFailure != null )
       _service.addEventListener( FaultEvent.FAULT, _onFailure );

     _service.send( sendData ); 

   }
  }
}             

No Response to "Servicing Restful CakePHP in Flex/Air"

 
powered by Blogger