【问题标题】:Errors in codeigniter-restserver librarycodeigniter-restserver 库中的错误
【发布时间】:2015-12-13 06:15:54
【问题描述】:

我想在我的 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);
          }
      }
    

运行 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 可用库的建议吗?类似于上面的库?

谢谢!

【问题讨论】:

    标签: rest codeigniter-restserver


    【解决方案1】:

    我使用那个库,工作得很好。我的建议是遵循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),这将是您的默认配置,您也可以根据需要进行更改。

    如果您使用我的回答仍然有错误,请告诉我:)

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 2011-10-28
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多