【发布时间】:2014-12-01 19:49:10
【问题描述】:
我有一个特定的任务,将 CakePHP Web 应用程序连接到远程 restful 服务器。我创建了一个数据源,读取方法效果很好,但是保存数据后的 api 返回一个处理过的数据数组。
寻找一种方法来返回数据数组并在控制器中使用。
我的控制器代码
public function admin_generate()
{
$data = $this->request->data;
$data['path'] = 'special/generate';
$this->Tool->create();
if($this->Tool->save($data)){
// handle response ????
}
$this->set('data',$data);
$this->set('_serialize','data');
}
在数据源文件中
public function create(Model $model, $fields = null, $values = null)
{
$data = array_combine($fields, $values);
$api = $this->config['api_path'].$data['path'].'?auth_key='.$this->config['auth_key'];
$json = $this->Http->post($api, $data);
$response = json_decode($json, true);
if (is_null($response)) {
$error = json_last_error();
throw new CakeException($error);
}
return $response; // ??????
}
有人可以告诉我在控制器中使用 api 响应数据的正确方法吗?
【问题讨论】:
标签: cakephp cakephp-2.0 datasource