Welcome Guest, Not a member yet? Register   Sign In
Errors in codeigniter-restserver library
#1

Hello,
I want to use restful in my ci 3.03 application:
1) I found this http://code.tutsplus.com/tutorials/worki...--net-8814 doc
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 :

PHP Code:
<?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($users200);
        }

        else
        {
            $this->response(NULL404);
        }
    


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

Code:
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?

Can you, please, to tell me advice some codeigniter 3 REST api workable library with similar interface like library above?

Thanks!
Reply
#2

This was written for a lower version of CI not for CI 3.0.X

For one the server extends the CI_Controller so that file would be placed in the core folder not libraries plus you would need to check for file names and classes being ucfirst.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Looks like this library is actually incompatible with ci303:
I tested simlar code in ci22 project and it dose not have any erros.
I copied library files from old ci22 project into c33 project ( * @version 3.0.0-pre ) and under ci33 it has errors
I moved thes files under application/core :

PHP Code:
require_once("application/core/REST22/REST_Controller.php");
require_once(
"application/core/REST22/Format.php"); 

Files name are ucfirst.

I tried to debug code of REST_Controller, but it apeared too difficult for me...
But in what are they different ? I know only  ucfirst difference, no more...

looks problems starts that format lib is not loaded? Is it difference?  

in file REST_Controller.php there is line :
PHP Code:
       $this->load->library('format'); 


But this library is not loaded ?
Reply
#4

If you are talking about https://github.com/chriskacerguis/codeig...restserver
it is compatible with CI 3.0.

But the way to install is a bit different from your way.
See https://github.com/chriskacerguis/codeig...stallation
Reply
#5

And I got error :


PHP Code:
SeverityNotice
Message
Undefined propertyUsers::$format
Filename
REST_3/REST_Controller.php
Line Number
734 

On first using of format object :

PHP Code:
           if (method_exists($this->format'to_' $this->response->format)) 

The header of my control is :

PHP Code:
require_once("application/core/REST_3/REST_Controller.php");
require_once(
"application/core/REST_3/Format.php");

class 
Users extends REST_Controller
{
   function users_get()
   {
    ... 
   
In header of application/core/REST_3/Format.php I set line :

PHP Code:
echo '<pre>INSIDE of Format::</pre>'


and I see this line in output! I mean this file is included in my project.
   
In file REST_Controller.php I see line with format library included and I show return value of

PHP Code:
$ret$this->load->library('format'); 

line
and its ouptut is :

PHP Code:
$ret::MY_Loader Object
(
   [_ci_loaded_sparks] => Array
       (
       )

   [_is_lt_210] => 1
   
[_ci_ob_level:protected] => 1
   
[_ci_view_paths:protected] => Array
       (
           [/mnt/diskD_Work/wwwroot/ci3/application/views/] => 1
       
)

   [_ci_library_paths:protected] => Array
       (
           [0] => /mnt/diskD_Work/wwwroot/ci3/application/
           [1] => /mnt/diskD_Work/wwwroot/ci3/system/
       )

   [_ci_model_paths:protected] => Array
       (
           [0] => /mnt/diskD_Work/wwwroot/ci3/application/
       )

   [_ci_helper_paths:protected] => Array
       (
           [0] => /mnt/diskD_Work/wwwroot/ci3/application/
           [1] => /mnt/diskD_Work/wwwroot/ci3/system/
       )

   [_ci_cached_vars:protected] => Array
       (
       )

   [_ci_classes:protected] => Array
       (
           [benchmark] => Benchmark
           
[hooks] => Hooks
           
[config] => Config
           
[log] => Log
           
[utf8] => Utf8
           
[uri] => URI
           
[router] => Router
           
[output] => Output
           
[security] => Security
           
[input] => Input
           
[lang] => Lang
           
[loader] => Loader
           
[appsmarty] => AppSmarty
           
[apputils] => AppUtils
           
[appauth] => AppAuth
       
)

   [_ci_models:protected] => Array
       (
       )

   [_ci_helpers:protected] => Array
       (
           [url_helper] => 1
           
[file_helper] => 1
           
[security_helper] => 1
       
)

   [_ci_varmap:protected] => Array
       (
           [unit_test] => unit
           
[user_agent] => agent
       
)



I see application and system dirs in paths...

Actually I tried to put
REST_Controller.php and Format.php files in different dirs(application/, application/libraries, application/core) with the same result : I mean Format.php is included in project, but line

PHP Code:
$this->format 

triggers error and I do not know why ?

If there is a way to debug how module is included in my project and which objects are attached to appilcation? I mean using of $this->format...
Reply
#6

1. Place application/libraries/Format.php and application/libraries/REST_Controller.php.
2. Remove the line "require_once("application/core/REST_3/Format.php");" in your controller.
Reply
#7

Thanks!
I moved files to  "application/libraries/" (not inside of any subdirectory) and it works, but I want to make some tests.
I copied methods users_post and users_get to my control :

Code:
<?php
require_once("application/libraries/REST_Controller.php");
class Users extends REST_Controller
{

   function __construct()
   {
       // Construct the parent class
       parent::__construct();

       // Configure limits on our controller methods
       // Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
       $this->methods['user_get']['limit'] = 500; // 500 requests per hour per user/key
       $this->methods['user_post']['limit'] = 100; // 100 requests per hour per user/key
       $this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
   }

   public function users_get()
   {
       // Users from a data store e.g. database
       $users = [
           ['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
           ['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
           ['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
       ];

       $id = $this->get('id');

       // If the id parameter doesn't exist return all the users

       if ($id === NULL)
       {
           // Check if the users data store contains users (in case the database result returns NULL)
           if ($users)
           {
               // Set the response and exit
               $this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
           }
           else
           {
               // Set the response and exit
               $this->response([
                   'status' => FALSE,
                   'message' => 'No users were found'
               ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
           }
       }

       // Find and return a single record for a particular user.

       $id = (int) $id;

       // Validate the id.
       if ($id <= 0)
       {
           // Invalid id, set the response and exit.
           $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
       }

       // Get the user from the array, using the id as key for retreival.
       // Usually a model is to be used for this.

       $user = NULL;

       if (!empty($users))
       {
           foreach ($users as $key => $value)
           {
               if (isset($value['id']) && $value['id'] === $id)
               {
                   $user = $value;
               }
           }
       }

       if (!empty($user))
       {
           $this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
       }
       else
       {
           $this->set_response([
               'status' => FALSE,
               'message' => 'User could not be found'
           ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
       }
   }

   public function users_post()
   {
       // $this->some_model->update_user( ... );
       $message = [
           'id' => 100, // Automatically generated by the model
           'name' => $this->post('name'),
           'email' => $this->post('email'),
           'message' => 'Added a resource'
       ];

       $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
   }

But running url "http://local-ci3.com/api/users" in browser I got

Code:
{"status":false,"error":"Unknown method"}

response.  Method users_get was not found! why?

I made JavaScript sample to test POST action :

Code:
function new_user() {
request_url = '{$base_url}api/user'
$("#div_request_url").html("POST : " + request_url);

var user_is_active = ''
if ($("#user_is_active_y").is(':checked')) {
user_is_active = 'Y'
}
if ($("#user_is_active_n").is(':checked')) {
user_is_active = 'N'
}

alert( "new_user  request_url  ::"+var_dump(request_url) ) // shows   http://local-ci3.com/api/user
$.ajax({
url: request_url,
dataType: "json",
type: "POST",
data: {
"name": $("#user_name").val(),
"is_active": user_is_active,
"activation_key": $("#user_activation_key").val(),
"login": $("#user_login").val(),
"password": $("#user_password").val(),
"active_till_date": $("#user_active_till_date").val(),
},
beforeSend: function (xhr) {
//xhr.setRequestHeader("X_REST_USERNAME", "<?php echo Yii::app()->params['RESTusername'] ?>");
//xhr.setRequestHeader("X_REST_PASSWORD", "<?php echo Yii::app()->params['RESTpassword'] ?>");
console.log(xhr);
},
success: function (data, textStatus, XMLHttpRequest) {

But in console of browser I saw :

Code:
POST http://local-ci3.com/api/user 404 (Not Found)


Which is the correct way? Have I to set some changes in application/config/routes.php or .htaccess files?
Reply
#8

(12-17-2015, 12:41 AM)mstdmstd Wrote:
Code:
POST http://local-ci3.com/api/user 404 (Not Found)

Your url is http://local-ci3.com/api/users. You missed s.

But I don't know why you get below when you get:
Code:
{"status":false,"error":"Unknown method"}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB