2017-04-18 140 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目錄下。