可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
回答1:
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);
}
}
回答2:
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;
回答3:
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
回答4:
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)
回答5:
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.
回答6:
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
回答7:
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!
回答8:
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
回答9:
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
.
回答10:
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 :)