【问题标题】:Post method in REST API using codeigniter使用 codeigniter 在 REST API 中发布方法
【发布时间】:2016-01-05 13:24:53
【问题描述】:

当我使用以下方法并将正文键作为fail(未定义键)传递时,一些值得到pass 消息作为回报并在表中插入空行,我该如何验证?

我在 REST API 中使用的函数,

function categories_POST() {
    $title = $this->post('title');
    $no = $this->post('no');
    $id= $this->post('id');

    $this->load->model('model_check');
    $msg = $this->model_check->addDetails($title , $no , $id);
    $this->response($msg);
}

我的模特,

function addDetails($x, $y, $z) {
    $check = "INSERT INTO categories (title,no,id) VALUES ('$x','$y','$z')";
    $query = $this->db->query($check);
    if($this->db->affected_rows() > 0) {
        return "pass";
    } else {
        return "fail";
    }
}

【问题讨论】:

  • 什么?你想验证什么?您是否尝试确定是否设置了帖子数据?使用伊塞特。使用空等。
  • @Kisaragi 我想使用title noid 传递变量,除此之外我需要停止将值传递给addDetails 函数
  • 只需先检查插入数据库的变量是否为空白,然后为特定变量添加响应为空白

标签: codeigniter api rest postman


【解决方案1】:

老实说,您最好使用查询构建器并(取决于您遵循的风格(胖/瘦控制器/模型))让模型处理 $this->post() 进行处理。

这是 Phil Sturgeons/Chris A 的休息服务器吗?

类似:

function categories_post() {  // doesn't need to be POST()

    $this->load->model('model_check');
    $msg = $this->model_check->addDetails()
    if ($msg)
    {
        $this->response([
            'status'            => TRUE,
            'message'           => 'pass'
          ], REST_Controller::OK);
    }
    // default to fail
   $this->response([
        'status'            => FALSE,
        'message'           => 'fail'
      ], REST_Controller::HTTP_BAD_REQUEST);
}

你的模特,

function addDetails() {
    // this only checks to see if they exist
    if (!$this->post() || !$this->post('x') || !$this->post('y') || !$this->post('z')) {
        return false;
    };
    $insert = array(
      'x' => $this->post('x'),
      'y' => $this->post('y'),
      'z' => $this->post('z'),
    );


    if($this->db->insert('categories', $insert))
    {
        return true;

    } 
    return false;  // defaults to false should the db be down

}

如果你的意思是 form_validation,你可以用这个代替上面的。

function addDetails() {

    $this->load->library('form_validation');
    $this->form_validation->set_rules('x', 'X', 'required');
    $this->form_validation->set_rules('y', 'Y', 'required');
    $this->form_validation->set_rules('z', 'Z', 'required');

    if ($this->form_validation->run() == true)
    {
        $insert = array(
          'x' => $this->post('x'),
          'y' => $this->post('y'),
          'z' => $this->post('z'),
        );

        if($this->db->insert('categories', $insert))
        {
            return true;

        }
    } 
    return false;  // defaults to false should the db be down

}

这很冗长,有更短的方法可以做到,但我宁愿让它容易弄清楚。

【讨论】:

    【解决方案2】:

    CodeIgniter中获取post值的两种方式

    $title = $this->input->post('title');
    $no = $this->input->post('no');
    $id= $this->input->post('id');
    
    
    
        $this->load->model('model_check');
        $msg = $this->model_check->addDetails($title , $no , $id);
        $this->response($msg);
    

    extract($_POST);
    

    然后直接访问帖子名

       $this->load->model('model_check');
        $msg = $this->model_check->addDetails($title , $no , $id);
        $this->response($msg);
    

    最好的方法是直接访问模型文件中的 post 值(不在控制器中)

    不需要在模型函数中传递 POST 值。

    如果您有更多疑问,请咨询我

    【讨论】:

    • 在不过滤任何值的情况下直接访问全局变量在任何地方都是不安全的
    • 我说你直接获取模型的值,然后使用 isset() 函数在模型中验证
    猜你喜欢
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2019-03-05
    • 2016-09-01
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多