微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sugarcrm rest api 实现流程

一.REST         方式实现流程

1.      入口:http://www.example.com/sugar/service/v2/rest.php

代码如下:

$webservice_class ='SugarRestService';

$webservice_path ='service/core/SugarRestService.PHP';//rest 核心类

$webservice_impl_class ='SugarWebServiceImplv4';//最核心的method所在

$registry_class ='registry'; //注册一些函数

$location ='/service/v4/rest.PHP';//代表本文件路径

$registry_path ='service/v4/registry.PHP';//注册函数文件路径

require_once('service/core/webservice.PHP');//启动程序

2.      启动程序:http://www.example.com/sugar/service/core/webservice.php

代码如下:

/*** This file intializethe service class and does all the setters based on the values provided insoap/rest entry point and calls serve method which takes the request and sendresponse back to the client  */

require_once($webservice_path);//include SugarRestService class

require_once($registry_path);..v2/registry.PHP

$url =$GLOBALS['sugar_config']['site_url'].$location; ../v2/rest.PHP

$service = new $webservice_class($url);new SugarRestService../v2/rest.PHP

$service->registerClass($registry_class);

$service->register();//想要排除的函数 exclude

$service->registerImplClass($webservice_impl_class);//参数  SugarWebServiceImplv4

$service->serve();//调用SugarRestServiceserve方法,这个方法很核心.

 

3.      Rest 核心类SugarRestService

代码如下:

   classSugarRestService extends SugarWebService{

       protected$implementationClass = 'SugarRestServiceImpl';

      

       function __construct($url){

               $this->restURL = $url;

              $this->serverClass=$this->_getTypeName(@$_REQUEST['input_type']);

//returnfor example: SugarRestJSON;

              require_once('service/core/REST/'.$this->serverClass . '.PHP');

       }

      

       functionregisterImplClass($className){

              $this->implementationClass= $className;

              $this->implementation= new $this->implementationClass();

//new SugarWebServiceImplv4()

              $this->server= new $this->serverClass($this->implementation);

              //new SugarRestJSON(new SugarWebServiceImplv4() )

              $this->server->registerd= $this->registeredFunc;

       }

      

       /**

        * It passes request data to REST server andsends response back to client

        * @access public

        */

       functionserve(){

              require_once('service/core/REST/'.$this->responseClass . '.PHP');

              $response  = $this->responseClass;//new SugarRestJSON

$responseServer= new $response($this->implementation);

              $this->server->faultServer= $responseServer;

              $responseServer->faultServer= $responseServer;

              $responseServer->generateResponse($this->server->serve());

              //$this->server->serve()调用的是SugarRestJSONserve()

       }

       functiongetServer(){

              return$this->server;

       }

4.      类SugarRestJSON分析

        function serve(){

               …

             

              $method = $_REQUEST['method'];

              $json= getJSONObj();

              $data =$json->decode($json_data);

              if(!is_array($data))$data= array($data);

              $res =call_user_func_array(array( $this->implementation,$method),$data);

              $GLOBALS['log']->info('End:SugarRestJSON->serve');

              return$res;

       }

5.      SugarRestJSON的基类SugarRest分析

function serve(){

   …

  $method = $_REQUEST['method'];

  return  $this->implementation->$method();

//implementation是类SugarWebServiceImplv4

}

6.      最核心类SugarWebServiceImpl分析

方法定义:

functionget_entries($session,$module_name,$ids,$select_fields,$link_name_to_fields_array){

$linkoutput_list = array();

$output_list = array();

$class_name = $beanList[$module_name];

require_once($beanFiles[$class_name]);

$temp = new $class_name();

foreach($ids as $id) {

        $seed = @clone($temp);

       $output_list[] =self::$helperObject->get_return_value_for_fields($seed,$select_fields);

        if (!empty($link_name_to_fields_array)) {

               $linkoutput_list[]= self::$helperObject->get_return_value_for_link_fields($seed,$link_name_to_fields_array);

        }

}

returnarray('entry_list'=>$output_list,'relationship_list' =>$linkoutput_list);

}

 

OVER

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐