我已经使用 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);
}
}
我基本上在一个控制器中完成了获取输入、输入验证、呈现错误或成功的视图以及其他内容。这样做的更好方法是什么,你建议我如何分解这个功能。有人说我应该在视图和控制器之间添加一个路由器/调度程序来处理, POST
,GET
但是我不知道该怎么做。COOKIE
SESSION
任何有助于我掌握正确做事方式的代码示例和建议都将被接受为答案。