Php 如何从codeigniter rest服务器向http响应添加位置头
我有一个codeigniter rest api,使用以下库创建: 对于我的POST方法,成功创建资源后,我希望包含一个位置头,其中包含一个URI以获取新创建/更新的资源。 我不清楚怎么做。 有人试过类似的东西吗 到目前为止,我只是将创建内容的细节添加到响应主体中。。。但我最终希望添加GET url作为位置头 到目前为止,我的逻辑是这样的:Php 如何从codeigniter rest服务器向http响应添加位置头,php,rest,http-headers,codeigniter-2,codeigniter-restserver,Php,Rest,Http Headers,Codeigniter 2,Codeigniter Restserver,我有一个codeigniter rest api,使用以下库创建: 对于我的POST方法,成功创建资源后,我希望包含一个位置头,其中包含一个URI以获取新创建/更新的资源。 我不清楚怎么做。 有人试过类似的东西吗 到目前为止,我只是将创建内容的细节添加到响应主体中。。。但我最终希望添加GET url作为位置头 到目前为止,我的逻辑是这样的: public function widget_post() { $new_widget_data = array(
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
}
如有任何建议,将不胜感激。谢谢。您是否尝试过重定向('widgets?widget='。$new\u widget\u data['name');
还是我没有抓住要点?我不相信当前版本的库提供了一种实现这一点的方法 您可以选择将php header()函数与REST\u控制器类提供的response()函数结合使用 标题(“位置:位置/收件人/新/资源”) 然后调用响应函数,确保设置了201代码。 $this->response($new\u widget\u data,201) 通常,默认情况下,位置头将触发重定向,因此请确保始终设置201代码
我想你不太明白我的问题。我不想将请求重定向到新创建的小部件。相反,当我使用创建的201发送响应时,我希望包含一个带有URI的位置头标记。这说明了吗?