Я создал веб-сервис, используя Restful Codeigniter. Но у меня проблема, когда мне нужно получить данные по id
это мой контроллер:
require APPPATH.'/libraries/REST_Controller.php';
class Example extends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('rs');
}
function user_get()
{
if(!$this->get('id'))
{
$this->response(NULL, 400);
}
$user = $this->rs>get_user($this->get('id'));
if($user)
{
$this->response($user, 200); // 200 being the HTTP response code
}
else
{
$this->response(array('error' => 'User could not be found'), 404);
}
}
function users_get()
{
$users = $this->rs->get_all();
if($users)
{
$this->response($users, 200); // 200 being the HTTP response code
}
else
{
$this->response(array('error' => 'Couldn't find any users!'), 404);
}
}
}
и это моя модель:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Rs extends CI_Model {
function __construct() {
parent::__construct();
}
function get_all()
{
$query = $this->db->get('user_tbl');
return $query->result();
}
function get_user($id)
{
$query = $this->db->get_where('user_tbl', array('id' => $id));
return $query->result();
}
}
когда я запускаю эту ссылку для получения данных по id: http://localhost/restserver/index.php/api/example/user/id/1
я получил ошибку:
Fatal error: Call to undefined function get_user() in /opt/lampp/htdocs/restserver/application/controllers/api/example.php on line 41
Но когда я запускаю: http://localhost/restserver/index.php/api/example/users/, он работает для меня, я получаю все данные без ошибок
почему я получаю сообщение об ошибке? почему функция не определена?
Вы пропустили дефис перед get_user(). Я предполагаю, что строка 41 - это
$user = $this->rs>get_user($this->get('id'));
Но это должно быть
$user = $this->rs->get_user($this->get('id')); //note the hyphen after 'rs
->rs>get_user
intead из->rs->get_user