Reputation: 168
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:
Upvotes: 3
Views: 35191
Reputation: 11
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 :)
Upvotes: 0
Reputation: 25
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
.
Upvotes: 0
Reputation: 3209
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
Upvotes: 1
Reputation: 51
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
Upvotes: 5
Reputation: 358
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!
Upvotes: 0
Reputation: 11
Without using Composer.
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)
Upvotes: 1
Reputation: 11
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
Upvotes: 0
Reputation: 329
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;
Upvotes: 9
Reputation: 168
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);
}
}
Upvotes: 6
Reputation: 6994
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.
Upvotes: 0