Codeigniter 如何禁用对回调函数的直接访问?

Codeigniter 如何禁用对回调函数的直接访问?,codeigniter,codeigniter-2,Codeigniter,Codeigniter 2,如何拒绝直接访问回调函数?我的建议是将验证规则塞进一个单独的文件中。CodeIgniter通过允许您在config/form\u validation.php中保存验证配置来支持这一点。请查看,特别是标有“将验证规则集保存到配置文件”的部分。 控制器的索引: config/form_validation.php 库/MY_Form_validation.php 您可以在方法名称前面加一个。来拒绝通过HTTP请求进行访问。对于像我这样为了自己的利益而在深夜编写代码的人,请确保您也更改了对set_r

如何拒绝直接访问回调函数?

我的建议是将验证规则塞进一个单独的文件中。CodeIgniter通过允许您在
config/form\u validation.php
中保存验证配置来支持这一点。请查看,特别是标有“将验证规则集保存到配置文件”的部分。

控制器的索引: config/form_validation.php 库/MY_Form_validation.php
您可以在方法名称前面加一个
来拒绝通过HTTP请求进行访问。

对于像我这样为了自己的利益而在深夜编写代码的人,请确保您也更改了对set_rules()中回调函数的调用,即回调函数名称(两个下划线)。我忘了做这个,想知道为什么它不起作用,doh!
<? if ( ! defined('BASEPATH')) exit();

    class Registration extends CI_Controller {

        public function __construct() {
            parent::__construct();
            $this->load->model('registration_model');
        }

        public function index() {
            $this->load->library('form_validation');

            $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email|callback_email_available');

            if($this->form_validation->run() == FALSE) {
                $this->load->view('registration');
            } else {
                $this->registration_model->add_user();
            }
        }

        # Check E-mail
        public function email_available($email) {
            $this->db->select('email');
            $this->db->where('email', $email);
            $query = $this->db->get('users');
            $result = $query->row();

            if(!empty($result)) {
                $this->form_validation->set_message('email_available', 'This e-mail belongs to another user.');
                return FALSE;
            } else {
                return TRUE;
            }
        }

    }
    ?>
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Registration::email_available()
Filename: controllers/registration.php

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: email
Filename: controllers/registration.php
public function index() {
    $this->load->library('form_validation');
    if($this->form_validation->run('submit_registration') == FALSE) {
        $this->load->view('registration');
    } 
    else{
        $this->registration_model->add_user();
    }
}
$config = array
(   
    'submit_registration' => array
    (
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'trim|required|valid_email|email_available'
        ),
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required|alpha_numeric|etc'
        )
    ),
    'some_other_submission' => array(
        array(
            'field' => 'somefield',
            'label' => 'SomeField',
            'rules' => 'some|rules'
        ),
        array(
            'field' => 'getit',
            'label' => 'Get The Point?',
            'rules' => 'trim'
        )
    )
);
class MY_Form_validation extends CI_Form_validation
{   
    function __construct($config = array()){
        parent::__construct($config);
    }

    function email_available($email){
        $CI =& get_instance();
        //check your database with the $CI variable...
       if(email_exists) return TRUE;
       else return FALSE;
    }
}