您可以给helper提供一个变量并更改它在CodeIgniter中使用的函数吗?


Can you give helpers a variable and change the function it uses in CodeIgniter

我正在创建一个表单,我想做一个自定义的错误日志,例如,当用户离开e-mail为空时,我想给我的error_helper变量email,发送给用户,他没有填写这个变量和日志,他离开这个空。我是codeIgniter的初学者,我已经阅读了手册,但我没有发现任何有用的东西。下面是我的代码:

    public function submitCheck() {
    $this->load->model("User");
    if(empty($post['email'] || 'email'==""))
    {
        echo "Email is empty."; //This will be changed to a view for user    
        $this->load->helper('error'); //Loading the helper
        errorLog('Firstname'); //Variable I want to give to the errorLog function.
        exit();
    }    

这里我给了errorLog一个变量,我想在error_helper

中使用它
    function errorLog(){         
     if(errorLog() == 'email') //Here I want to change the error function based on what variable I give to errorLog
     {
        log_message('error', 'Email was empty');
        exit();
     }
}  

这是可能的或其他方式如何我可以改变函数基于什么变量我给我的errorLog。提前感谢

您需要将parameter传递到辅助函数

控制器文件

public function submitCheck() {
    $this->load->model("User");
    if(empty($post['email']))
    {
       // echo "Email is empty."; //This will be changed to a view for user    
        $this->load->helper('error'); //Loading the helper
     echo  $msg= errorLog('Firstname'); //Variable I want to give to the errorLog function.
    }  

Helper文件

    function errorLog($var){ // get the variable thet you have pass from your helper        
         if($var == 'email') //Here I want to change the error function based on what variable I give to errorLog
         {
            return log_message('error', 'Email was empty');// here you use return type
         }
         if($var == 'test') 
         {
            return log_message('error', 'Test was empty');// here you use return type
         }
}

还加载controller constructor文件或autoload.php文件中的帮助器

函数中的读取参数:

    function errorLog($var){         
     if($var== 'email') //Here I want to change the error function based on what variable I give to errorLog
     {
        log_message('error', 'Email was empty');
        exit();
     }
}