2017-04-18 148 views
0

我想问一下,如果在codeigniter中扩展控制器的正确方法是什么。因为现在我遇到Fatal error: Class 'ApiController' not found in这两个文件都驻留在同一目录api文件夹中。这里是我的代码:在Codeigniter 2.0中扩展控制器

<?php 
//EchelonApiController.php 
class EchelonApiController extends ApiController { 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function index(){ 
     echo 'asd'; 
    } 
} 


<?php 
// ApiController.php 
class ApiController extends CI_Controller { 

    public $table; 

    public function __construct(){ 

     parent::__construct(); 

     $this->load->models("api/".$table); 
    } 

    public function index(){ 

    } 
} 

回答

1

在扩展它之前,您需要包含基类文件。

<?php 
//EchelonApiController.php 

require "path/to/file/ApiController.php"; 

class EchelonApiController extends ApiController { 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function index(){ 
     echo 'asd'; 
    } 
} 

如果ApiController.phpEchelonApiController.php都处于同一个文件,你可以直接使用

require "ApiController.php"; 

否则只是增加正确的路径与APPPATH不断的帮助。

0

把你的ApiController放在application/core目录下。