1

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

  1. 我找到了这个tutplus 教程
  2. 我下载了codeigniter-restserver-master.zip文件并将Format.php文件复制REST_Controller.php(@version 3.0.0)/application/libraries/REST directory
  3. 我创建了控制应用程序/控制器/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");

在我的控制器中,但我仍然收到如下错误:消息:未定义属性:用户::$格式。我试图查看这个库的代码,并在数据转换为 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 可用库的建议吗?这些库具有类似的接口,如上面的库?

谢谢!

4

1 回答 1

2

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

你也错误地放置了lib文件:

  • 教程说:

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

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

无需包含,format因为在第 407 行,lib 将加载它。也很高兴知道第 404 行它将加载配置 ( application/config/rest.php),这将是您的默认配置,您也可以根据需要进行更改。

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

于 2016-02-28T14:11:22.117 回答