实际开发中,API当执行成功时,大多都需要返回一个结果集给请求端,当我们不需要返回结果集时,系统会默认取/restful/config.php
文件中的数据结构配置项
对应的set
内容作为返回结果。
我们也可以通过\x\Restful::data(结果集)
方法,指定返回结果集。
依赖类:\x\Restful
方法名:data()
参数:
mixed $data
返回值:\x\Restful
对象本身
示例代码:
namespace app\http;
use x\controller\Http;
/**
* @Controller(prefix="")
*/
class Index extends Http
{
/**
* @RequestMapping(route="/", method="get", title="主页")
*/
public function index() {
// 使用XML返回结构
// 抛出/restful/default/code.php中的SUCCESS状态码
// 抛出/restful/default/msg.php中的SUCCESS['create_shop_success']提示语
// 抛出自定义结果集
return \x\Restful::type('xml')
->code(\x\Restful::SUCCESS())
->msg('create_shop_success')
->data([
'name' => 'sw-x'
])->callback();
}
}