我有一个使用此库创建的 codeigniter rest api: https ://github.com/chriskacerguis/codeigniter-restserver
对于我的 POST 方法,一旦成功创建资源,我想包含一个包含 URI的位置标头,以获取新创建/更新的资源。我不清楚如何做到这一点。有没有人尝试过类似的东西?
到目前为止,我只是将已创建内容的详细信息添加到响应正文中……但我最终希望将 GET url 添加为位置标头。
到目前为止,这就是我的逻辑:
public function widget_post()
{
$new_widget_data = array(
'location' =>$this->post('location'),
'name' => $this->post('name'),
'cost' => $this->post('cost')
);
// validate post data
if (!$this->isvalidpost($new_widget_data)) {
$this->response("Invalid data",400);
}
// add to database
$res = $this->widget_model->notify_servers($new_widget_data);
$new_widget_data['results']=$res;
if ($res) {
//append GET url to return value
$new_widget_data['GET']=base_url()."/index.php/widgets/widget/".$new_widget_data['name'];
$this->response($new_widget_data,201); //HTTP Created 201
} else {
$this->response("Unable to process request",500); //HTTP Internal server error
}
任何建议,将不胜感激。谢谢。