[英]Codeigniter Restful API not working
I have a Codeigniter setup where I installed the Restful API thing. 我有一个Codeigniter设置,在其中安装了Restful API。 I created an API-folder in my
application->controller->api
and after that i created an API which looks like this: 我在
application->controller->api
创建了一个API文件夹,然后创建了一个如下所示的API:
<?php
require(APPPATH.'libraries/REST_Controller.php');
class Allartists extends REST_Controller{
function artists_get()
{
if(!$this->get('artist_id'))
{
$this->response(NULL, 400);
}
$artists = $this->artist_model->get( $this->get('artist_id') );
if($artists)
{
$this->response($artists, 200);
}
else
{
$this->response(array('error' => 'Couldn\'t find any artists!'), 404);
}
}
?>
In my application->models
-folder I have the file artist_model.php
which looks like this: 在我的
application->models
-folder中,有文件artist_model.php
,如下所示:
<?php
Class artist_model extends CI_Model
{
function get_all_artists(){
$this->db->select('*');
$this->db->from('artists');
return $this->db->get();
}
}
?>
So, when I type http://localhost/myprojects/ci/index.php/api/Allartists/artists/
I get 400 - Bad Request
-error... when I type http://localhost/myprojects/ci/index.php/api/Allartists/artists/artist_id/100
I get PHP error Undefined property: Allartists::$artist_model
- So what is going on here? 因此,当我输入
http://localhost/myprojects/ci/index.php/api/Allartists/artists/
我得到400 - Bad Request
-错误...当我输入http://localhost/myprojects/ci/index.php/api/Allartists/artists/artist_id/100
我收到PHP错误Undefined property: Allartists::$artist_model
那么这是怎么回事?
You need to load your model. 您需要加载模型。 Add a constructor to
Allartists
and load it. 向
Allartists
添加一个构造Allartists
并加载它。
class Allartists extends REST_Controller{
function __construct(){
parent::__construct();
$this->load->model('Artist_model');
}
// ...
}
PS Your model needs to have the 1st letter in its class name capitalized (see: http://ellislab.com/codeigniter/user-guide/general/models.html ): PS:您的模型需要在其类名中使用第一个字母大写(请参阅: http : //ellislab.com/codeigniter/user-guide/general/models.html ):
class Artist_model extends CI_Model{
// ...
}
UPDATE: You are looking for $this->get('artist_id')
. 更新:您正在寻找
$this->get('artist_id')
。 This will never be set as you are not sending a $_GET['artist_id']
value ( ?artist_id=100
in the URL). 由于您没有发送
$_GET['artist_id']
值(URL中的?artist_id=100
,因此将永远不会设置该值。 You need to get the $artist_id
another way in your controller. 您需要以其他方式在控制器中获取
$artist_id
。
function artists_get($artist_id=FALSE)
{
if($artist_id === FALSE)
{
$this->response(NULL, 400);
}
$artists = $this->artist_model->get( $artist_id );
if($artists)
{
$this->response($artists, 200);
}
else
{
$this->response(array('error' => 'Couldn\'t find any artists!'), 404);
}
}
Then go to: 然后转到:
http://localhost/myprojects/ci/index.php/api/Allartists/artists/100
Or , keeping your current code, you could simply change the URL to: 或者 ,保留当前代码,您只需将URL更改为:
http://localhost/myprojects/ci/index.php/api/Allartists/artists?artist_id=100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.