douchuo0730 2014-02-21 12:14
浏览 38

如何在codeigniter中创建用于获取所有数据并通过id获取数据的rest方法?

I want to perform CRUD operations through REST, I am implementing this in codeigniter, The code whatever I pasted here is working, but I have to handle a way to fetch all the datas from the database and also a way to fetch the data by id. Is there any best way to do this?

Backbone.js

(function(){
    Backbone.emulateHTTP = true;
    //Backbone.emulateJSON = true;
    window.App = {
        Models: {},
        Collections: {},
        Views: {},
        Router: {}
    };

    App.Models.Task = Backbone.Model.extend({
        defaults: {
            title: '',
            done: 0
        },
        urlRoot: 'index.php/taskController/task'
    });
})();

Controller

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 require(APPPATH.'libraries/REST_Controller.php');
 class taskController extends REST_Controller {
 public function task_get($id){
    $this->load->model('Task', 'task');
    $data['task'] = $this->task->findbyid($id);
}

public function tasks_get(){
    $this->load->model('Task','task');
    $data['task'] = $this->task->find();
    $this->response($data,200);
}

public function task_put($id)
{
    # code...
    $this->load->model('Task', 'task');
    $data = json_decode(file_get_contents('php://input'), true);
    // $data['title'] = $var['title'];
    // $data['done'] = $var['done'];
    echo var_dump($data);       
    $data['task'] = $this->task->updatebyid($id,$data);
    //$this->response($data,200);
}

public function task_delete($id){
    $this->load->model('Task','task');
    $data['task'] = $this->task->delete($id);
}

public function task_post(){
    $this->load->model('Task','task');
    $data = json_decode(file_get_contents('php://input'),true);
    return $data['task'] = $this->task->create($data);
}

}
  • 写回答

2条回答 默认 最新

  • douhan6738 2014-02-21 12:20
    关注

    I use /get/id for the items and /list/number_to_show/limit

    So add a list_get($number, $limit)

    method

    if code for a /get/id is no id is passed, Send the entire lot ?

    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题