Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter RESTful API - {"status":false,"error":"Unknown method."}

I've managed to setup the RESTful API in my Codeigniter Application. Now I want to get some data from my MySQL database, so in my Codeigniter models folder I have created a model called category_model.php:

<?php
  Class category_model extends CI_Model {
      var $table_name = 'category';

     function get_all_categories()
     {
       $this->db->select('*');
       $this->db->from($this->table_name);
       return $this->db->get();
     }
 }
?>

Then in the Codeigniter controller-folder I created a category.php file:

<?php

 include(APPPATH.'libraries/REST_Controller.php');

 class Category extends REST_Controller {

  function __construct()
    {
        parent::__construct();
        $this->load->model('category_model');
    }

  function category_get()
    {
        $data = $this->category_model->get_all_categories();
        $this->response($data);
    }

 }

?>

Now, when I enter http://localhost/myproejcts/ci/index.php/category/category - I get the error {"status":false,"error":"Unknown method."} ??

what is the issue?

[UPDATE] = I get the same error when setting function index_post()

like image 899
SHT Avatar asked Jan 13 '14 13:01

SHT


1 Answers

Depends of your HTTP request to your controller "Category" it will call related method:

public function index_get()
{   
    echo "GET_request";
}   

public function index_post()
{
    echo "POST_request";
}

public function index_put()
{
    echo "PUT_request";
}

public function index_patch()
{
    echo "PATCH_request";
}

public function index_delete()
{
    echo "DELETE_request";
}

So, rename your method to 'index_get'

like image 133
Alex Dom Avatar answered Sep 21 '22 08:09

Alex Dom