Php Codeigniter Restful API不工作
我有一个Codeigniter设置,在那里我安装了RESTfulAPI。我在我的Php Codeigniter Restful API不工作,php,codeigniter,rest,Php,Codeigniter,Rest,我有一个Codeigniter设置,在那里我安装了RESTfulAPI。我在我的应用程序->控制器->API中创建了一个API文件夹,之后我创建了一个如下所示的API: <?php require(APPPATH.'libraries/REST_Controller.php'); class Allartists extends REST_Controller{ function artists_get() { if(!$this->get('artist_id'))
应用程序->控制器->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);
}
}
?>
<?php
Class artist_model extends CI_Model
{
function get_all_artists(){
$this->db->select('*');
$this->db->from('artists');
return $this->db->get();
}
}
?>
所以,当我输入
http://localhost/myprojects/ci/index.php/api/Allartists/artists/
我收到400-错误请求
-错误。。。当我键入http://localhost/myprojects/ci/index.php/api/Allartists/artists/artist_id/100
我得到PHP错误未定义属性:Allartists::$artist\u model
-那么这里发生了什么 您需要加载您的模型。将构造函数添加到Allartists
并加载它
class Allartists extends REST_Controller{
function __construct(){
parent::__construct();
$this->load->model('Artist_model');
}
// ...
}
另外,您的模型需要将类名中的第一个字母大写(请参见:):
更新:您正在寻找$this->get('artist\u id')
。这将永远不会被设置,因为您没有发送$\u GET['artist\u id']
值(?artist\u id=100
,在URL中)。您需要以另一种方式在控制器中获取$artist\u 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);
}
}
然后转到:
http://localhost/myprojects/ci/index.php/api/Allartists/artists/100
或者,保留当前代码,只需将URL更改为:
http://localhost/myprojects/ci/index.php/api/Allartists/artists?artist_id=100
试试$this->Artist\u model->get
?您得到的是相同的错误还是不同的错误?$this->get('artist_id')
将始终为false。您没有传递$\u GET['artist\u id']
值。这仍然不起作用。。。可能是艺术家的id吗?它是DB表中的一列artists
它怎么不工作?你还看到同样的错误吗?您是否尝试过var\u dump($artist\u id)
和/或var\u dump($artist\u id==FALSE)
查看其中的内容?在if($artist\u id==FALSE)
之前。确保您要进入/api/Allartists/artists/100
。
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);
}
}