Php Codeigniter rest api没有';t以set_响应终止
我正在使用Codeigniter rest api 我所知道的是,Php Codeigniter rest api没有';t以set_响应终止,php,rest,codeigniter,Php,Rest,Codeigniter,我正在使用Codeigniter rest api 我所知道的是,$this->set\u response终止执行并返回带有状态代码的结果 但当我尝试在构造函数中执行此操作时,它会忽略它并继续执行目标方法 我的控制器 class Posts extends REST_Controller { public $user_id; public $ret_arr = [ 'status' => FALSE, 'message' => ''
$this->set\u response
终止执行并返回带有状态代码的结果
但当我尝试在构造函数中执行此操作时,它会忽略它并继续执行目标方法
我的控制器
class Posts extends REST_Controller
{
public $user_id;
public $ret_arr = [
'status' => FALSE,
'message' => ''
];
function __construct()
{
// Construct the parent class
parent::__construct();
$this->load->model('api/User_model');
$this->load->model('api/Entity_model');
$this->load->model('api/Post_model');
$token = $this->input->get_request_header('token', TRUE);
if($token!=NULL) {
$this->get_user_id($token);
}
else
{
$this->ret_arr['status'] = FALSE;
$this->ret_arr['message'] = 'Token Not Valid';
$this->set_response($this->ret_arr, REST_Controller::HTTP_UNAUTHORIZED);
}
}
public function get_user_id($token)
{
$this->user_id = $this->User_model->check_token($token);
if ($this->user_id == FALSE||$token==''||$token===null) {
$this->ret_arr['status'] = FALSE;
$this->ret_arr['message'] = 'Token Not Valid';
$this->set_response($this->ret_arr, REST_Controller::HTTP_UNAUTHORIZED);
}
}
public function addpost_post()
{
//$post_string = $this->request->body[0];
//$post = json_decode($post_string, true);
$post=$this->input->post();
if (isset($post['post_type']) && $post['post_type'] != '') {
$text = $this->input->post('text');
$type = $post['post_type'];
if ($type == 'text') {
$this->add_post_text($text);
} elseif ($type == 'url') {
} elseif ($type == 'image') {
} elseif ($type == 'video') {
}
} else {
$this->ret_arr['status'] = FALSE;
$this->ret_arr['message'] = 'Missing Post Type';
$this->set_response($this->ret_arr, REST_Controller::HTTP_UNPROCESSABLE_ENTITY);
}
}
}
当我请求这个URL时,头中没有令牌,也没有发送数据,它会返回
localhost:8080/myProject/api/Posts/addpost
{“status”:false,“message”:“Missing Post Type”}和状态422
但它必须回归
{“状态”:false,“消息”:“令牌无效”}和状态401
更新(已解决)
我用
$this->response()
而不是$this->set\u response()
解决了这个问题,请按照下面的描述更改我们的代码
从
到
请更改我们的代码,如下所述 从 到
我用$this->response()代替$this->set_response(),谢谢你:)我用$this->response()代替$this->set_response(),谢谢你:)解决了这个问题
$post=$this->input->post();
$post=$this->post();