0

我已经使用 codeIgniter 5 个月了,我想改进我编写代码的方式。这是来自控制器的用户方法:

function create_user(){ 

         $user_data = array(
            'username' => 'Username', 'firstname' => 'Firstname',
            'middlename' => 'Middlename', 'lastname' => 'Lastname',
            'password' => 'Password', 'department' => 'Department', 
            'role' => 'Role'
        );

        foreach ($user_data as $key => $value) {
            $this->form_validation->set_rules($key, $value, 'required|trim');
        }

        if ($this->form_validation->run() == FALSE) {
            $departments = $this->user_model->list_departments();
            $it_roles = $this->user_model->list_roles(1);
            $tc_roles = $this->user_model->list_roles(2);
            $assessor_roles = $this->user_model->list_roles(3);

            $data['data'] = array('departments' => $departments, 'it_roles' => $it_roles, 'tc_roles' => $tc_roles, 'assessor_roles' => $assessor_roles);

            $data['content'] = 'admin/create_user';

            parent::error_alert();
            $this->load->view($this->_at, $data);

        } else {
            $username = $this->input->post('username');
            $salt = $this->bcrypt->getSalt();
            $hashed_password = $this->bcrypt->hash($this->input->post('password'), $salt);
            $fname = $this->input->post('firstname');
            $mname = $this->input->post('middlename');
            $lname = $this->input->post('lastname');
            $department = $this->input->post('department');
            $role = $this->input->post('role');

            $user_login     = array($username, $hashed_password, $salt);
            $user_profile   = array($fname, $mname, $lname);
            $this->user_model->register_user($user_login, $user_profile, $department, $role);

            $data['content'] = 'admin/view_user';

            parent::success_alert(4, 'User Sucessfully Registered!', 'You may now login using your account');


            $data['data'] = array('username' => $username, 'fname' => $fname, 'mname' => $mname, 'lname' => $lname, 'department' => $department, 'role' => $role);
            $this->load->view($this->_at, $data);
        }

    }

我基本上在一个控制器中完成了获取输入、输入验证、呈现错误或成功的视图以及其他内容。这样做的更好方法是什么,你建议我如何分解这个功能。有人说我应该在视图和控制器之间添加一个路由器/调度程序来处理, POSTGET但是我不知道该怎么做。COOKIESESSION

任何有助于我掌握正确做事方式的代码示例和建议都将被接受为答案。

4

2 回答 2

2

您还需要在 POST 请求后使用重定向,以免重复提交

function create_user() {

    $params = $this->input->post();

    if( !$this->user_model->valid_user_params( $params, $errors ) )
    {
        //Place errors in flash data
        //redirect and exit
    }

    if( !$this->user_model->register_user($params, $errors) )
    {
        //Place errors in flash data
        //redirect and exit
    }

    //redirect and exit

}

function created_user() {
    //read flash data
    //show some views with the errors or success message
}
于 2012-07-17T14:05:32.343 回答
1

我做了一些小的改动:不设置不必要的变量,将用户数据分组到一个数组中等等。这就是我要做的方式。我会说,除此之外,你一路走好。

<?php 
function create_user()
{ 
     $user_data = array ('username' => 'Username', 'firstname' => 'Firstname', 'middlename' => 'Middlename', 'lastname' => 'Lastname', 'password' => 'Password', 'department' => 'Department', 'role' => 'Role');

    foreach ($user_data as $key => $value) {
        $this->form_validation->set_rules($key, $value, 'required|trim');
    }

    if ($this->form_validation->run() == FALSE)
    {

        $data['data'] = array(  'departments'   => $this->user_model->list_departments(), 
                                'it_roles'      => $this->user_model->list_roles(1), 
                                'tc_roles'      => $this->user_model->list_roles(2), 
                                'assessor_roles'=> this->user_model->list_roles(3)
                        );

        $data['content'] = 'admin/create_user';

        parent::error_alert();
        $this->load->view($this->_at, $data);

    } 
    else 
    {
        $salt    = $this->bcrypt->getSalt();

        $user['username']       = $this->input->post('username');
        $user['hashed_password']= $this->bcrypt->hash($this->input->post('password'), $salt);
        $user['fname']          = $this->input->post('firstname');
        $user['mname']          = $this->input->post('middlename');
        $user['lname']          = $this->input->post('lastname');
        $user['department']     = $this->input->post('department');
        $user['role']           = $this->input->post('role');

        $user_login     = array($user['username'], $user['hashed_password'], $salt);
        $user_profile   = array($user['fname'], $user['mname'], $user['lname']);
        $this->user_model->register_user($user_login, $user_profile, $department, $role);

        $data['content'] = 'admin/view_user';

        parent::success_alert(4, 'User Sucessfully Registered!', 'You may now login using your account');

        unset($user['hashed_password']); // Just to be sure 
        $this->load->view($this->_at, $user);
    }

}
于 2012-07-17T14:10:40.377 回答