1

I want to use restful in my ci 3.03 application:

  1. I found this tutplus tutorial
  2. I downloaded codeigniter-restserver-master.zip file and copied Format.php and REST_Controller.php(@version 3.0.0) files into /application/libraries/REST directory
  3. I created control application/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);
          }
      }
    

AND RUNNING URL http://local-ci3.com/api/users I got many errors:

A PHP Error was encountered

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

Actually I wanted to get some workable library to help me with REST api creation. I think that is preferable way istead of making from zero. But is this library not workable or does it needs for some fixing? Sorry, what I missed is if this library only for ci 2?

I made search on this forum and found such hint :

I have the same problem when I load both Format.php and Rest_Controller.php into a controller. After have a quick glance at Format.php, it appears to be a standalone format conversion helper. Try to just load Rest_Controller.php and see if your problem goes away.

I commented line

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

in my controller, but I still get errors like : Message: Undefined property: Users::$format. I tried to review code of this library and see that invalid block when data are converted to json format, line 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;
    }
}

If I tried to commented this block, but get error

Message: Array to string conversion

Looks like data are not converted in this case...

Is is possible to fix these errors? Or can you, please, to tell me advice some codeigniter 3 REST api workable library with similar interface like library above?

Thanks!

1 Answer 1

2

I use that lib, work just fine. My suggestion is follow the more relevant installation instruction on github .

you also wrong place the lib file :

  • Tutorial say :

    require(APPPATH'.libraries/REST_Controller.php');
    
  • You try :

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

No need to include the format because on line 407 the lib will load it. And also good to know on line 404 it will load the configuration (application/config/rest.php) it will be your default configuration, and also you can change it to suit your need.

Please let me know if you still got error using my answer :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.