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()
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With