3

I'm having a problem with the popular REST API codeigniter maintained by Chris Kacerguis.I created a Datadiri controller that looks like this:

<?php
require (APPPATH.'/libraries/REST_Controller.php');

class Datadiri extends REST_Controller{
    function __construct($config = 'rest'){
        parent::__construct($config);
    }
    //tampilkan data
    function index(){
        $buku = $this->db->get('perpustakaan');
        $this->response($buku, 200);
    }
}

This is Rest_controller.php https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php

But Still same, the error Class 'REST_Controller' not found. how can i solve this?

Fatal error: Class 'REST_Controller' not found in C:\xampp\htdocs\CodeIgniter\application\controllers\datadiri.php on line 4 A PHP Error was encountered

Severity: Error

Message: Class 'REST_Controller' not found

Filename: controllers/datadiri.php

Line Number: 4

Backtrace:

9
  • Can you print APPPATH.'/libraries/REST_Controller.php to make sure it's correct? What does it say? Commented Jan 6, 2017 at 3:21
  • ok, edit my question. @sergChernata Commented Jan 6, 2017 at 3:26
  • I don't see it. Can you do - print APPPATH.'/libraries/REST_Controller.php'; and paste the result in your question? Commented Jan 6, 2017 at 3:28
  • it say 404 Page Not Found Commented Jan 6, 2017 at 3:30
  • That makes so sense, it should print a string. Commented Jan 6, 2017 at 3:32

10 Answers 10

9

I had the same issue and adding the namespace after the require() statement in the controller solved the problem.

After this:

require (APPPATH.'/libraries/REST_Controller.php');

Add this:

// use namespace
use Restserver\Libraries\REST_Controller;
Sign up to request clarification or add additional context in comments.

Comments

6

Finally the problem solved. iam edit application/config/config.php from : $config['subclass_prefix'] = 'MY_'; to : $config['subclass_prefix'] = 'REST_';

And Datadiri.php like this :

<?php
  use Restserver\Libraries\REST_Controller;
   class Datadiri extends REST_Controller{
       function __construct(){
           parent:: __construct();
       }
       function index_get(){
           $buku = $this->db->get('perpustakaan')->result();
           $this->response($buku, 200);
       }
   }

Comments

5

in your controller api php make sure have this,

require APPPATH . '/libraries/REST_Controller.php';
use Restserver\Libraries\REST_Controller;

and then go to your Rest_Controller and make sure you have this

use CI_Controller;
require APPPATH . 'libraries/Format.php';

Hope this help you

Comments

1

Without using Composer.

  • Download / extract CI at your project directory
  • Download codeigniter-restserver from github (https://github.com/chriskacerguis/codeigniter-restserver)
  • Extract codeigniter-restserver over (at the same directory of ) codeigniter, overwritting CodeIgniter files (and dirs) WITH codeigniter-restserver files
  • Edit file controllers\api\Example.php Just before line "class Example extends REST_Controller { " add:

require APPPATH . 'libraries/REST_Controller.php'; require APPPATH . 'libraries/Format.php';

After that, it will look like:

... require APPPATH . 'libraries/REST_Controller.php'; require APPPATH . 'libraries/Format.php';

class Example extends REST_Controller {

function __construct()
{

...

For testing: - Open the project in your browser - At the welcome screen, click on REST Server Tests - At the next screen, click on Users (- defaulting to JSON)

Comments

1

I had the same issue while implementing this in my own Codeigniter project. I just copied Format.php and REST_Controller.php files in Libraries Folder.
After placing these files in Libraries Folder, you have to change
namespace Restserver\Libraries;
to
namespace YOUR_PROJECT_NAME\Libraries;
in both files (Format.php and REST_controller.php).
and in your controller which extends REST_Controller put these three lines right after php tag

use YOUR_PROJECT_NAME\Libraries\REST_Controller;
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

Hope it will help someone. Thanks

Comments

0

Lets try as below....

 <?php
   require('application/libraries/REST_Controller.php');

    class Datadiri extends REST_Controller{
        function __construct($config = 'rest'){
            parent::__construct($config);
        }
        //tampilkan data
        function index(){
            $buku = $this->db->get('perpustakaan');
            $this->response($buku, 200);
        }
    }

OR

Put the REST_Controller.php in application/core then just

<?php

class Datadiri extends REST_Controller{
    function __construct($config = 'rest'){
        parent::__construct($config);
    }
    //tampilkan data
    function index(){
        $buku = $this->db->get('perpustakaan');
        $this->response($buku, 200);
    }
}

Not need to include file placed in core folder.

8 Comments

thanks for answer, but still same, cannot find REST_Controller
try from both ways.
are you sure every files are in required folder according to Chris Kacerguis.
i am pretty sure, i get from github
lets try once require (APPPATH.'libraries/REST_Controller.php');
|
0

this library will move into Composer, so you will need to run

composer require chriskacerguis/codeigniter-restserver

and change $config['COMPOSER_AUTOLOAD'] to true in config file

Comments

0

Its a two way reference issue, you can basicly solve it by doing the following two steps:

1-In your api class add the following code at the beginning:

use Restserver\Libraries\REST_Controller;
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

class Example extends REST_Controller {..

2-In the file : application/libraries/REST_Controller.php add :

use CI_Controller;

before

abstract class REST_Controller extends CI_Controller {

Cheers!

Comments

0

Best way is to install using composer, then load composer autoload file in codeigniter and use namespace use Restserver\Libraries\REST_Controller; in you controller which extends REST_Controller.

Comments

0

add this...

require(APPPATH.'/libraries/REST_Controller.php');
   use Restserver\Libraries\REST_Controller;
   use CI_Controller;
   require APPPATH . 'libraries/Format.php';

Hopefully this will work :)

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.