codeigniter-restserver库中的错误

时间:2015-12-13 06:15:54

标签: rest codeigniter-restserver

我想在我的ci 3.03应用程序中使用restful:

  1. 我发现了这个tutplus tutorial
  2. 我下载了codeigniter-restserver-master.zip个文件,并将Format.phpREST_Controller.php(@version 3.0.0)个文件复制到/application/libraries/REST directory
  3. 我创建了控制应用程序/ controllers / api / Users.php:

    require_once("application/libraries/REST/REST_Controller.php");
    require_once("application/libraries/REST/Format.php");
    
    class Users extends REST_Controller
    {
    //protected $rest_format = 'json';
    
      function users_get()
      {
          //$users = $this->user_model->get_all();
          $filter_username= $this->get('filter_username');
          $filter_user_group= $this->get('filter_user_group');
          $filter_active= $this->get('filter_active');
          $sort= $this->get('sort');
          $sort_direction= $this->get('sort_direction');
    
          //, $filter_user_group, $filter_active, $sort, $sort_direction
          $users_list = $this->muser->getUsersList(false, ''/*, $filter_username, $filter_user_group, $filter_active, $sort, $sort_direction, ''*/);
          echo '<pre>'.count($users_list).'::$users_lists::'.print_r($users_list,true).'</pre>';
    
    
          if($users_list)
          {
              $this->response($users, 200);
          }
    
          else
          {
              $this->response(NULL, 404);
          }
      }
    
  4. AND RUNNING URL http://local-ci3.com/api/users我收到了很多错误:

    遇到PHP错误

    Severity: Notice
    Message: Undefined property: Users::$format
    Filename: REST/REST_Controller.php
    Line Number: 734
    Backtrace:
    File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
    Line: 734
    Function: _error_handler
    File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
    Line: 649
    Function: response
    File: /mnt/diskD_Work/wwwroot/ci3/index.php
    Line: 292
    Function: require_once
    
    A PHP Error was encountered
    Severity: Notice
    Message: Undefined property: Users::$format
    Filename: REST/REST_Controller.php
    Line Number: 752
    Backtrace:
    File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
    Line: 752
    Function: _error_handler
    File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
    Line: 649
    Function: response
    File: /mnt/diskD_Work/wwwroot/ci3/index.php
    Line: 292
    Function: require_once
    

    实际上我想得到一些可行的库来帮助我创建REST api。我认为这是优先的方式,而不是从零开始。 但是这个库是不可行还是需要修复?对不起,我错过的是这个库只适用于ci 2吗?

    我在这个论坛上搜索并发现了这样的提示:

      

    当我加载Format.php和时,我遇到了同样的问题   将Rest_Controller.php放入控制器。快速浏览一下后   Format.php,它似乎是一个独立的格式转换助手。   尝试加载Rest_Controller.php并查看问题是否存在   程。

    我评论了一行

    //require_once("application/libraries/REST/Format.php");
    

    在我的控制器中,但我仍然会收到如下错误: 消息:未定义的属性:Users :: $ format。 我试图检查这个库的代码,并在数据转换为json格式时看到无效块,第731-757行:

    elseif ($data !== NULL)
    {
        // If the format method exists, call and return the output in that format
        if (method_exists($this->format, 'to_' . $this->response->format))
        {
            // Set the format header
            $this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
            $output = $this->format->factory($data)->{'to_' . $this->response->format}();
    
            // An array must be parsed as a string, so as not to cause an array to string error
            // Json is the most appropriate form for such a datatype
            if ($this->response->format === 'array')
            {
                $output = $this->format->factory($output)->{'to_json'}();
            }
        }
        else
        {
            // If an array or object, then parse as a json, so as to be a 'string'
            if (is_array($data) || is_object($data))
            {
                $data = $this->format->factory($data)->{'to_json'}();
            }
    
            // Format is not supported, so output the raw data as a string
            $output = $data;
        }
    }
    

    如果我试图评论此块,但会收到错误

    Message: Array to string conversion
    

    在这种情况下,似乎没有转换数据......

    是否可以修复这些错误? 或者,您能告诉我一些codeigniter 3 REST api可操作的库以及类似于上面的库的界面吗?

    谢谢!

1 个答案:

答案 0 :(得分:2)

我使用那个lib,工作得很好。我的建议是遵循github上更相关的安装说明。

你错误地放置了lib文件:

  • 教程说:

    require(APPPATH'.libraries/REST_Controller.php');
    
  • 你试试:

    require_once("application/libraries/REST/REST_Controller.php");
    require_once("application/libraries/REST/Format.php");
    

无需包含format,因为在line 407上lib会加载它。并且在第404行也很有用,它将加载配置(application/config/rest.php)它将是您的默认配置,并且您也可以根据需要进行更改。

如果您使用我的答案仍然出错,请告诉我。)