在Codeigniter Restful服务器中加载模型功能

时间:2013-03-01 08:09:19

标签: php api codeigniter curl

最近我开始使用宽度Codeigniter Restful服务器。我使用以下脚本:https://github.com/philsturgeon/codeigniter-restserver

如果我试图获取我的模型的属性,Rest服务器返回NULL。在普通的CI控制器中,此功能有效。代码是:

require(APPPATH.'/libraries/REST_Controller.php'); 
class Api extends REST_Controller
{
    function user_post()  
    {  
        $username = $this->post('username');
        $password = $this->post('password');
        $company_code = $this->post('company_code');
        $key = $this->post('key');
        $this->CI =& get_instance();

        // Load user model
        $this->load->model('User_model');
        $this->load->model('Api_model');

        // Ip adres remote server
        $server_ip  = $_SERVER['REMOTE_ADDR'];

        // Ophalen data
        $user = $this->User_model->getApiLogin($username,$password);

最后一行的错误:$ user = $ this-> User_model-> getApiLogin($ username,$ password);

通过Curl调用Api:

             $key = 'Test';
         $company_code = 'econome';  
         $username = 'jellepals';  
         $password = 'test';  

         $curl_handle = curl_init();  
         curl_setopt($curl_handle, CURLOPT_URL, 'http://jelle.testapiserver.nl/api/user/format/json');
         //curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
         //curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);             
         curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);  
         curl_setopt($curl_handle, CURLOPT_POST, 1);  
         curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(  
             'key' => $key,  
             'username' => $username,
             'password' => $password));  
         $buffer = curl_exec($curl_handle);  
         curl_close($curl_handle);  
         $result = json_decode($buffer);  
         var_dump($result);

有人知道如何在Restserver中调用模型函数吗? 谢谢!

3 个答案:

答案 0 :(得分:0)

就像这样

$this->load->model('category_model');
$categories = $this->category_model->get_categories_api($language_code);

尝试加载这样的模型(模型的第一个字母很简单)

    $this->load->model('user_model');
    $this->load->model('api_model');

答案 1 :(得分:0)

我认为问题出在我的数据库连接中。有人在db类和名为Db_handler.php的模型之间构建了一个额外的层。当从api调用时,这个层显然不起作用。通过这一层,我们可以使用多个数据库。添加时:

   var $db;

    public function __construct() {
        parent::__construct();
        $this->db = $this->load->database($this->config->item('default', 'database'), TRUE);
    }

对于我的模型,connexxion工作正常。

感谢MDeSilva评论。

答案 2 :(得分:0)

简单使用

            $CI = get_instance();
            $CI->load->model('B_db');
            $result=$CI->B_db->run_query($query);