CI3.0 在控制器和模块之间传递变量


CI3.0 passing variable between controller and module

我有这个方法:

public function signup_validation()   {
//definizioni setting per email di conferma
  $emailsetting = array('mailtype' => 'html' );
  $emailfrom = 'misure@lagiumentabardata.netsons.org';
  $emailsubject ='Conferma registrazione';
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required|trim|valid_email|is_unique[user.email]'); //check se la mail è davvero una mail valida e se esiste gia nel database     $this->form_validation->set_rules('password','Password','required|trim');   $this->form_validation->set_rules('cpassword','Conferma Password','required|trim|matches[password]');
$this->form_validation->set_message('is_unique','email già esistente nel nostro database'); //override messaggio di errore
//check se il form è valido allora passa altrimenti ritorna al form.    if ($this->form_validation->run() == true) {
    //generare key
  $key = sha1(uniqid());
  //inviare una mail all'utente
  $this->load->library('email', $emailsetting); //caricamento libreria
  $this->load->model('model_users'); //caricamento modello per il controllo del db utente
  $this->email->from($emailfrom,'La Giumenta Bardata');
  $this->email->to($this->input->post('email'));
  $this->email->subject($emailsubject);
  $emailmessage = "<h1>ciao mondo</h1>
                  <a href='".base_url()."signup/register_user/$key'>click qui</a>"; //fare riferimento alla funzione register_user
  $this->email->message($emailmessage);
  //controllo scrittura dell'utente nel db
  if ($this->model_users->add_temp_user($key) == true) { //passaggio chiave al modello che si occupa di aggiungerlo al DB
    //invio email utente
    if ($this->email->send() == true) {
      echo "email inviata correttamente";
    } else { echo "errore nell'invio stronzo";}
  } else { echo "problemi nell'inserimento del db"; }
} else {
    echo '<script>alert("TU NON PUOI PASSARE!");</script>';
    $this->registrazione();     }   }

signup.php控制器中。首先我不明白为什么如果我在设置所有变量($emailsetting$emailfrom等)的地方移动$emailmessage变量。我收到一个错误,指出$emailmessage变量未定义-.-

但是,真正的问题是我无法将 $key 变量传递给模块:

 public function add_temp_user($key){
 $data = array('email' => $this->input->post('email'),
               'nome' => $this->input->post('nome'),
               'cognome' => $this->input->post('cognome'),
               'password' => md5($this->input->post('password')),
               'userkey'=>$key
               );
  $query = $this->db->insert('temp_users',$data);  //generazione query
  //check se la query è stata eseguita correttamente
  if ($query) {
    return true;
  } else {
    return false;
  }   
 }

如何将变量从控制器传递到模块?我尝试了一切,从将$key设置为公共到设置__construct,没有任何效果......当应用程序需要调用模型时,我有一个未定义的$key变量。

感谢您的帮助

您可以将$emailmessage =移动到设置其他内容的位置。但是,$key = sha1(uniqid());行必须在这些任务之前。

我在没有您的数据库的情况下尽我所能对此进行了测试。似乎有效。 无论如何,$key都进入了模型。

请注意,我构建链接<a href=...略有不同,更好地利用了 Codeigniter 的能力。

 public function signup_validation()
  {
    //generare key
    $key = sha1(uniqid());
//definizioni setting per email di conferma
    $emailsetting = array('mailtype' => 'html');
    $emailfrom = 'misure@lagiumentabardata.netsons.org';
    $emailsubject = 'Conferma registrazione';
    $emailmessage = "<h1>ciao mondo</h1>
      <a href='".base_url("signup/register_user/$key")."'>click qui</a>"; //fare riferimento alla funzione register_user
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]'); //check se la mail è davvero una mail valida e se esiste gia nel database     $this->form_validation->set_rules('password','Password','required|trim');   $this->form_validation->set_rules('cpassword','Conferma Password','required|trim|matches[password]');
    $this->form_validation->set_message('is_unique', 'email già esistente nel nostro database'); //override messaggio di errore
//check se il form è valido allora passa altrimenti ritorna al form.    if ($this->form_validation->run() == true) {
    //inviare una mail all'utente
    $this->load->library('email', $emailsetting); //caricamento libreria
    $this->load->model('model_users'); //caricamento modello per il controllo del db utente
    //use chaining here, much more efficient
    $this->email
        ->from($emailfrom, 'La Giumenta Bardata')
        ->to($this->input->post('email'))
        ->subject($emailsubject)
        ->message($emailmessage);
    //controllo scrittura dell'utente nel db
    if($this->model_users->add_temp_user($key))
    { //passaggio chiave al modello che si occupa di aggiungerlo al DB
      //invio email utente
      if($this->email->send() == true)
      {
        echo "email inviata correttamente";
      }
      else
      {
        echo "errore nell'invio stronzo";
      }
    }
    else
    {
      echo "problemi nell'inserimento del db";
    }
//these lines are not connected to anything as you have them in your question. 
// So I have commented them out
//else {
//    echo '<script>alert("TU NON PUOI PASSARE!");</script>';
//    $this->registrazione();     }   
  }

这是模型函数。

我像这样input->post(NULL, TRUE);捕获输入,因为它一次捕获所有输入。这消除了对input->的多次调用。使用第二个参数 = TRUE,输入将被清理。

由于db->insert()返回 true 或 false,因此不需要 if/then 语句。

  public function add_temp_user($key)
  {
    $posted = $this->input->post(NULL, TRUE);
    $data = array('email' => $posted('email'),
        'nome' => $posted('nome'),
        'cognome' => $posted('cognome'),
        'password' => md5($posted('password')),
        'userkey' => $key
    );
    //tornare successo o il fallimento di ricerca
    return $this->db->insert('temp_users', $data);  //generazione query
  }