控制器帮助和功能



我有一个通用的create函数,它可以向数据库提交一个新用户,这很好。我陷入困境的原因如下。

  • 我知道我需要注册的用户在帐户登录之前点击了电子邮件中的链接运行create函数时,如何将其实现到我的if statement

  • 如果激活过程中有任何事情是正确的或错误的,我有点困惑于如何设置我的错误。我目前使用$this->form_validation->set_message();设置消息我需要使用set_flashdata();吗;?如何将这些反映到视图中

当我create为新用户时,默认情况下userActive字段设置为0,并且默认组设置为users

控制器:

   <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {

    public function index()
    {
        $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
        $data['pageTitle'] = "Create User";
        $this->load->view('frontend/assets/header', $data);
        $this->load->view('frontend/users', $data);
        $this->load->view('frontend/assets/footer');
    }
    public function create(){   
        //If form validation fails load previous page with errors else do the job and insert data into db
        if($this->form_validation->run('createUser') == FALSE)
        {
            $data['success'] = "";
        }else{
            $username = $this->input->post('userName');
            $password = $this->input->post('userPassword');
            $firstname = $this->input->post('userFirstName');
            $lastname = $this->input->post('userLastName');
            $email = $this->input->post('userEmail');
            $passwordHash = $this->encrypt->sha1($password); // Lets encrypt the password why sha1?  MD5 is for tossers
            $activateCode = $this->_activateCode(10);
            // If the data is correct follow through with db insert
            if($this->users_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$activateCode))
            {
                $data['success'] = TRUE;
                redirect('frontend/users/create','refresh');
            }
        }
        $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
        $data['pageTitle'] = "Create User";
        $this->load->view('frontend/assets/header', $data);
        $this->load->view('frontend/user_create', $data);
        $this->load->view('admin/assets/footer');
        echo get_class($this);
        var_dump(method_exists($this, '_activateCode'));
    }
    function _userRegEmail($activateCode,$email,$firstname,$lastname){
        $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
        $data['companyEmail'] = $this->core_model->companyDetails()->coreCompanyEmail;
        $data['companyContact'] = $this->core_model->companyDetails()->coreContactName;
        $data['firstName'] = $firstName;
        $data['lastName'] = $lastname;
        $data['email'] = $email;
        $data['activateCode'] = $activateCode;
        $this->email->from($this->core_model->companyDetails()->coreCompanyEmail, $this->core_model->companyDetails()->coreCompanyName);
        $this->email->to($email);
        $this->email->subject($this->core_model->companyDetails()->coreCompanyName, 'User Registration Confirmation');
        $messageContent= $this->load->view('email_templates/userReg','', TRUE);
        $this->email->message($messageContent);
        //$this->email->send();
    }
    function usersconfirm(){
        $activateCode = $this->uri->segment(3);
        if($activateCode == '')
        {
            $this->form_validation->set_message('userConfirmError', 'Sorry you did not have a correct Activation Code.');
        }
            $userConfirmed = $this->users_model->confirm_user($activateCode);
            if($userConfirmed){
                $this->form_validation->set_message('userConfirmed', 'Thanks your account is now active you may login!');
            }else{
                $this->form_validation->set_message('userRecord', 'I am sorry we do not have any details with that Activation Code');
            }
    }
     function _username_check($username)
    {
        if($this->users_model->username_taken($username))
        {
            $this->form_validation->set_message('username_check', 'Sorry the username %s is taken!');
            return FALSE;
        }else{
            return TRUE;
        }
    }
    function _email_check($email)
    {
        if($this->users_model->email_check($email))
        {
            $this->form_validation->set_message('email_check','Sorry there is already a user with this %s');
            return FALSE;
        }else{
            return TRUE;
        }
    }
   function _activateCode($length)
    {
        return random_string('alnum', $length);
    }
}
/* End of file users.php */
/* Location: ./application/controllers/users.php */

您可以通过在if语句中检查数据库中的userActive来确定用户是否单击了激活链接。

当然,你可以使用闪存数据。您可以通过以下方式检索闪存数据:$this->session->flashdata('item');以回显到视图。

请参阅http://codeigniter.com/user_guide/libraries/sessions.html>闪存数据

最新更新