2011-05-15 55 views
1

在我的一個控制器中,我使用codeigniter的表單驗證助手。我的驗證規則之一是我創建的自定義函數,返回true或false。 問題是,這個驗證函數是在控制器類中聲明的。我如何阻止用戶瀏覽我的驗證功能作爲控制器操作(mysite/mycontroller/my_callback_func)?如何創建不可訪問的控制器操作?

P.S. - 試圖將該函數設置爲私有的,但我得到一個錯誤,證明助手無法訪問它。

回答

3

只需用下劃線啓動函數/方法名稱即可。

直出./system/core/CodeIgniter.php

/* 
* ------------------------------------------------------ 
* Security check 
* ------------------------------------------------------ 
* 
* None of the functions in the app controller or the 
* loader class can be called via the URI, nor can 
* controller functions that begin with an underscore 
*/ 
    $class = $RTR->fetch_class(); 
    $method = $RTR->fetch_method(); 

    if (! class_exists($class) 
     OR strncmp($method, '_', 1) == 0 
     OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller'))) 
     ) 
    { 
     show_404("{$class}/{$method}"); 
    } 
+0

這是我要做的事還和工作得很好。它也在文檔中列出:http://codeigniter.com/user_guide/general/controllers.html#private – 2011-05-16 00:29:20

0

我會改爲幫助功能。爲此,請將文件放入您的system/application/myval_helper.php,然後在其中添加您的功能。然後,只需撥打:

$this->load->helper('myval'); 

而且您將可以訪問此功能。另一種方法是簡單地使用.htaccess來阻止該確切的URL。

1

簡單地定義你的函數私人,把一個uderscore以前生產函數的名稱,例如:

class Some_class extends CI_Controller{ 

    private function _some_method(){} 
} 
相關問題