我正在SugarCRM中构建一个自定义REST端点,该端点将两个模块连接在一起并返回结果。我需要允许用户以查询参数的形式传递可选数据。当前,端点如下所示:
http://base-url.com/api/customer/{customer_id}/branch/{branch_id}/offset/{offset}
但是,这要求我传递一个偏移值。相反,我希望端点看起来像
http://base-url.com/api/customer/{customer_id}/branch/{branch_id}?offset={offset}
我检查了SugarCRM开发人员文档,也在线检查,但是找不到使用查询参数的确定示例。
下面是我的代码。此代码示例与上面列出的第一个端点匹配。我的目标是将offset参数修改为查询字符串,而不是路径变量
<?php
if( !defined('sugarEntry') || !sugarEntry )
die('Not A Valid Entry Point');
class LinkLeadsApi extends SugarApi
{
public function registerApiRest()
{
return array(
'LinkLeadsEndpoint' => array(
'reqType' => 'GET',
'noLoginRequired' => false,
'path' => array('customer', '?', 'branch', '?', 'offset', '?'),
'pathVars' => array('customer', 'customer_id', 'branch', 'branch_id', 'offset', 'offset_num'),
'method' => 'GetLinkLeads',
'shortHelp' => 'Retrieve Leads for Latham Link',
'longHelp' => 'Retrieve Leads for Latham Link'
)
);
}
public function GetLinkLeads($api, $args)
{
$seed = BeanFactory::newBean('w002_ConsumerLeads');
$q = new SugarQuery();
$q->from($seed);
$q->limit($args['offset_num']);
return $q->execute();
}
}
?>
答案 0 :(得分:1)
您的查询字符串仍然可以通过$ _REQUEST变量在同一PHP接口中访问,但是它们也可以在$ args中使用:
public function GetLinkLeads($api, $args)
{
$GLOBALS['log']->fatal("args: " . print_r($args, true));
$GLOBALS['log']->fatal("request: " . print_r($_REQUEST, true));
}
url:{sugar} / rest / v10 / customer / 1 / branch / 2 / offset / 3?qs = 4
sugarcrm.log
Wed Apr 24 12:06:27 2019 [19200][-none-][FATAL] args: Array
(
[__sugar_url] => v10/customer/1/branch/2/offset/3
[qs] => 4
[customer] => customer
[customer_id] => 1
[branch] => branch
[branch_id] => 2
[offset] => offset
[offset_num] => 3
)
Wed Apr 24 12:06:27 2019 [19200][-none-][FATAL] request: Array
(
[__sugar_url] => v10/customer/1/branch/2/offset/3
[qs] => 4
)