创建自定义端点

SugarCRM 7.x 的一个功能是能够轻松添加和扩展自定义端点,以满足你的需求。

在此示例中,我们将创建一些自定义端点以返回有关请求的一些数据。

此自定义文件放在 custom/clients/base/api/DescriptionAPI.php 中。

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

//You need to inherit the SugarApi Class to
class DescriptionApi extends SugarApi
{
    public function registerApiRest()
    {
        return array(

            //Define a key for the array
            'DescribeRequest' => array(

                //Array of the acceptable kinds of requests for this method
                'reqType' => array('GET','POST','PUT','DELETE'),

                //If true, anyone can access. If false, only authenticated users.
                'noLoginRequired' => true,

                //Here is the path to access the endpoint, in this case: Describe/Request
                'path' => array('Describe', 'Request'),

                //Specify an empty string for the path variables
                'pathVars' => array('', ''),

                //method to call
                'method' => 'DescribeMyRequest',

                //A small description, displayed in rest/v10/help page
                'shortHelp' => 'Describes your Request Method',

                //Further help, displayed when drilling down into the help page
                'longHelp' => 'custom/clients/base/api/help/DescribeRequestHelp.html',
            ),
            //Here's another entry with some more in depth information
            'DescribeIncludingArgs' => array(
                'reqType' => array('GET','POST','PUT','DELETE'),
                'noLoginRequired' => true,

                //This time, we'll include a third element with a ?
                //So now the path is Describe/Request/{dataFromURL}
                'path' => array('Describe', 'Request', '?'),

                //Here, we specify the key for accessing that data within the function
                'pathVars' => array('', '','dataFromURL'),
                
                'method' => 'DescribeMyRequestIncludingArguments',
                'shortHelp' => 'Describes the request you sent, including method, URL parameters, and request body',
                'longHelp' => 'custom/clients/base/api/help/DescribeIncludingArgsHelp.html',
            ),
        );

    }

    /**
     * Your custom logic goes in here.
     */
    public function DescribeMyRequest($api, $args)
    {
        //Find out the request method sent
        $requestType = $_SERVER['REQUEST_METHOD'];

        return "You sent a $requestType request.";
    }

    /**
     * Here is the second function
     */
    public function DescribeMyRequestIncludingArguments($api, $args)
    {
        //Find out the request method sent
        $requestType = $_SERVER['REQUEST_METHOD'];

        //Get the data included in the URL parameter
        $data = $args['dataFromURL'];

        //Read from the request body
        $body = file_get_contents('php://input');

        return "You sent a $requestType request including the header argument: `$data` and the body: `$body`";
    }

}

添加此文件后,你需要执行修复和重建,以便 Sugar 正确注册你的端点。

之后,如果你在浏览器中导航到 rest / v10 / Describe / Request,你应该看到:

“你发了 GET 请求。”

现在,如果你使用 REST 客户端发送 POST 请求,并发送一些数据,例如:POST rest/v10/Describe/Request/Stuff with body {"key":"value"},你应该收到:

“你发送了一个 POST 请求,包括标题参数:Stuff 和 body:{\"key\":\"value\"}