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

25,820

Solution 1

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'

Solution 2

I think your problem is the name of controller is the same with the name of method try to make a test:

if the name of your controller is:

class Test extends CI_Controller{
    //your method name is different from the name of controller class
    public function testget_get(){ 
        echo $this->response(array('test'=> 'test'), 200);
    }
}

I have experienced this problem on hmvc structure.

Solution 3

while calling the method don't use _get,_post .. say for example you have a method users_get

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    require APPPATH . '/libraries/REST_Controller.php';

    class Student extends REST_Controller {
        function __construct() {
            // Construct the parent class
            parent::__construct();
        }

        public function users_get() {
            $this->response("my first api");
        }
    }

?>   

let us call this method => "your base url"/student/users

Share:
25,820
SHT
Author by

SHT

Updated on July 27, 2020

Comments

  • SHT
    SHT almost 4 years

    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()

  • kev_m
    kev_m about 8 years
    so for example the name of my controller is controllername all i need to do to my functions is controllername_get controllername_post etc?
  • onalbi
    onalbi about 8 years
    no must be different, if your controller is called [Test] your method name should be different from your controller name excluding postfix [_get, _post].. example [name_get]. So $this->test->name_get() and not $this->test->test_get().
  • Rohan Khude
    Rohan Khude over 7 years
    Please provide a description for your answer.
  • Lefty
    Lefty almost 5 years
    Actually this works.. . I have a method called: allUsers_get() I call it on post man as: localhost/myapp/api/Example/allUsers