,如果你想自定義您的CodeIgniter應用程序的要求,試試這個: 您必須創建一個應用程序/掛鉤命名Ajax_only.php鉤文件夾
class Ajax_only {
private $_controllers = [];
private $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function eval_request() {
$controller = $this->CI->router->fetch_class();
$method = $this->CI->router->fetch_method();
if (array_key_exists($controller, $this->_controllers) && $this->CI->input->is_ajax_request() === FALSE ) {
if (($this->_controllers[ $controller ] === TRUE || (is_array($this->_controllers[ $controller ]) && array_key_exists($method, $this->_controllers[ $controller ]) && $this->_controllers[ $controller ][ $method ] === TRUE))) {
show_404();
}
}
}
}
/*Examples
* $_controllers = [
* 'my_controller_name' => TRUE //all methods must be ajax
* 'my_controller_name => [
* 'method_name' => TRUE //only the selected methods must be ajax
* ]
* ]
*/
和配置您的application/config/hooks.php文件
$hook['post_controller_constructor'] = array(
'class' => 'Ajax_only',
'function' => 'eval_request',
'filename' => 'Ajax_only.php',
'filepath' => 'hooks'
);
太棒了!我知道我可能會錯過一些東西......非常感謝,夥計! – 2012-07-27 12:05:26
is_ajax_request()不是一個安全措施。有一個類似的答案,404返回,而這將避免給人一個誤導的印象。 (HTTP/1.1「當服務器不希望準確揭示爲什麼請求被拒絕,或者沒有其他響應適用時,通常使用此狀態代碼」)http://stackoverflow.com/questions/6555652/controller-方法,這只是被稱爲by-ajax-make-private/8072539#8072539 – sourcejedi 2012-10-03 11:04:23
我通常使用加載方法來加載文件,我如何防止這些文件直接加載? – 2016-08-12 04:07:46