2011-05-07 73 views

回答

8

這個你最好的選擇是明確地把它們寫入到一個新的配置文件。

$config['controllers'] = array(
    'blog', 
    'events', 
    'news', // etc. 
); 

否則,您將掃描將耗盡資源的目錄。但是你可以做這樣的:

$controllers = array(); 
    $this->load->helper('file'); 

    // Scan files in the /application/controllers directory 
    // Set the second param to TRUE or remove it if you 
    // don't have controllers in sub directories 
    $files = get_dir_file_info(APPPATH.'controllers', FALSE); 

    // Loop through file names removing .php extension 
    foreach (array_keys($files) as $file) 
    { 
     $controllers[] = str_replace(EXT, '', $file); 
    } 
    print_r($controllers); // Array with all our controllers 

由於文件名匹配控制器的名字,現在你應該有你的控制器的陣列。儘管有幾個原因,但這並不完美,但應該適用於大多數設置。

就我個人而言,我使用高度修改的目錄結構,所以這不適用於我,還有一些控制器我也想忽略。另一個選擇是將結果緩存到文件中,但這是一個單獨的教程。

我強烈建議在配置文件中定義它們,這樣您就可以存儲與您的訪問控制直接相關的其他有用信息,並避免遞歸掃描目錄的巨大開銷。

0

另一種方法是你可能做到這一點是通過創建一個接口,然後你可以驗證系統內檢查,例如,創建一個類,像這樣:

interface IAuthorizationRequired 
{ 
    public function __auth(); 
} 

現在創建您的控制器(僞)

class BlogController extends CI_Controller implements IAuthorizationRequired 
{ 
    public function __auth() 
    { 
      /*Redirect or Custom*/ 
    } 
} 

和您的授權模塊中,你加載電流控制器,並執行follewing:

if(($controller instanceof IAuthorizationRequired) && method_exists(array($controller,'__auth'))) 
{ 
    $authed = $controller->__auth(); 
    if(!$authed) 
    { 
     echo 'Authorization Failed'; 
     exit; 
    } 
} 

在2.0之內,您可以重寫基本控制器,併爲其添加方法__auth,然後只檢查接口並運行身份驗證。

相關問題