{" status":false,"错误":"未知方法"}错误CodeIgniter RESTful API

时间:2016-12-02 07:30:17

标签: php codeigniter

<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
    // Call the Model constructor
    parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
} 

function restclient()
{

$this->load->library('rest', array(
    'server' => 'http://localhost/CodeIg/index.php/quiz/'
));

$userr = $this->rest->get('user','','json');

echo $userr;
}

}
?>

如果我在浏览器中输入http://localhost/CodeIg/index.php/quiz/user,我就可以获得JSON输出,但如果我输入http://localhost/CodeIg/index.php/quiz/restclient,则会出现此错误:{"status":false,"error":"Unknown method"}

我尝试将get更改为post但仍然出现同样的错误。

我在此页面https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814上提到了这一点。

2 个答案:

答案 0 :(得分:0)

你把我压在GitHub上,尽管我至少在4年内没有使用或甚至考虑过这段代码。

https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680

这是触发该错误的地方。在那里或.bulb { -fx-fill: lightslategray; } .bulb:on { -fx-fill: gold; } 之间扔几个断点,直到看到导致问题的原因。

你可能想要离开CodeIgniter,并使用更积极维护的东西,如SlimPHPLumen

答案 1 :(得分:0)

首先我想要你已经加载了rest api并创建了你的控制器测验作为api来调用,你只能创建你的函数,如user_get或restclient_get,并以你正在做的方式访问它们。只需更改你的函数名称restclient到restclient_get然后它将调用它而不是此时它甚至没有运行。