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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21092792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:49:18  来源:igfitidea点击:

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

phpmysqlcodeigniterrest

提问by SHT

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:

我已经设法在我的 Codeigniter 应用程序中设置了 RESTful API。现在我想从我的 MySQL 数据库中获取一些数据,所以在我的 Codeigniter 模型文件夹中我创建了一个名为 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:

然后在 Codeigniter 控制器文件夹中我创建了一个 category.php 文件:

<?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."}??

现在,当我输入时http://localhost/myproejcts/ci/index.php/category/category- 我收到错误{"status":false,"error":"Unknown method."}??

what is the issue?

这是什么问题?

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

[UPDATE]= 我在设置时遇到同样的错误function index_post()

回答by Alex Dom

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

取决于您对控制器“类别”的 HTTP 请求,它将调用相关方法:

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'

因此,将您的方法重命名为“index_get”

回答by onalbi

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.

我在 hmvc 结构上遇到过这个问题。

回答by Virendra Pawar

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

调用该方法时不要使用 _get,_post .. 比如说你有一个方法 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

让我们调用此方法 =>“您的基本网址”/student/users

回答by Virendra Pawar

mention the details you want to fetch from database and enter the correct name in the database file located in /restapi/application/config/databasefile and run the code.

提及要从数据库中获取的详细信息,并在位于/restapi/application/config/database文件中的数据库文件中输入正确的名称并运行代码。

this error occurs due to the incorrect database name

由于数据库名称不正确而发生此错误

**<?php
  Class category_model extends CI_Model {
      var $table_name = 'category';
     function get_all_categories()
     {
       $this->db->select('name, email');
       $this->db->from($this->table_name);
       return $this->db->get();
     }
 }
?>**

回答by K Jagannath Reddy

Please change your class name to start with capital letter and make sure your file name also starts with capital letter

请更改您的班级名称以大写字母开头,并确保您的文件名也以大写字母开头

<?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();
     }
 }
?>

回答by Manoj Patidar

Just write below lines in your controller's top section.

只需在控制器顶部写下几行即可。

if (!defined('BASEPATH')) exit('No direct script access allowed');

use Restserver\Libraries\REST_Controller;

require APPPATH . 'libraries/REST_Controller.php';

require APPPATH . 'libraries/Format.php';