如何在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);
}

}

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 ?

The principle of REST is that the CRUD actions are represented by the HTTP verbs. GET = select, PUT = update, POST = create and DELETE = delete.

You use nouns in your URL to represent your resources (e.g. tasks).

From your CI code it looks like you use always GET and have verbs+nouns in your URLs.

In REST, to get all tasks you would need to do GET http://example.com/tasks. To get one specific task you would need to do GET http://example.com/tasks/1234

Please read http://info.apigee.com/Portals/62317/docs/web%20api.pdf to understand the principle.