我有一个像这样的控制器:
public function index() {
$data['content'] = 'homepage/login';
$this -> load -> view('templates/no_jsTemplate', $data);
}
其中index()是登录页面(任何人都会看到的第一页)
现在索引页面有一个将其发布数据发送到的表单:
public function login_user() {
$this -> load -> library('form_validation');
if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {
$this -> index();
} else {
$this -> load -> model('m_homepage');
$this -> m_homepapge -> login_user();
}
}
是否可以压缩这个?我觉得每页有两个控制器功能是否过多?
我试过了:
public function index() {
if (!$this -> input -> post('login_user')) { // check if submit is clicked, if not then just load the data array and show the view
$data['content'] = 'homepage/login';
$this -> load -> view('templates/no_jsTemplate', $data);
} else { // if it is clicked load the library and then do the validation and then load the model if validation passes and then do the login_user business calculation
$this -> load -> library('form_validation');
if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {
$this -> index();
} else {
$this -> load -> model('m_homepage');
$this -> m_homepapge -> login_user();
}
}
}
但是这样做会让我陷入无限循环之中。我已经构建了其他应用程序,我将有一个函数来加载视图,然后是一个控制器将数据发送到模型的函数。我只是想学习其他更好的做事方式。我的成绩反映在这个项目上。我的主要目标是尽可能 DRY 。我想尝试编写更少的代码以获得更多爆炸效果。希望这不是要求太多。
答案 0 :(得分:1)
尝试此希望它可行,可能需要进行一些调整,将表单提交URL
更改为index
函数:
<强> EDITED 强>
public function index() {
if($this->input->post(null)){
$this -> load -> library('form_validation');
if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {
//$this -> index();
redirect('controller/index', 'refresh');
} else {
$this -> load -> model('m_homepage');
$this -> m_homepapge -> login_user();
redirect('controller/method', 'refresh'); //ADDED
}
}else{
$data['content'] = 'homepage/login';
$this -> load -> view('templates/no_jsTemplate', $data);
}
}
post(null)
返回所有内容,仅用于检查帖子数组是否包含内容。
答案 1 :(得分:0)
在以下几行中,我将指出逻辑,您可能需要考虑保持代码 DRY 。
请避免复制并粘贴这些行,您应该对其进行操作以使其适用于您的项目。还仔细阅读内部的评论。
public function index()
{
if (isset($_POST['login_user'])) {
// Load validation library
$this -> load -> library('form_validation');
// Set your validation rules by using $config array
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE) {
// Store the validation error messages
$data['error'] = validation_errors();
} else {
// Do the login process
$this->load->model('m_homepage');
$this->m_homepapge->login_user();
// After user is logged in, do you need to say Welcome to him/her?
// You might want to fetch user's name from database, so:
// $data['success'] = "Hello $name, You're logged in."
//
// If you need to show the `Successfully` message to user at once
// Store the message in flashdata:
// $this->session->set_flashdata('success', 'message');
//
// Do you want to redirect user to somewhere else?
// redirect('route', 'refresh');
}
} else {
// This method is called directly and/or without any login POST data.
// Do you need to do somethin special in this case? put your logic here.
}
// Always send a view to the user,
// If user doesn't send the POST data, it shows the login form,
// Or if user made a mistake during filling the login form,
// you can show the `error` to him/her and also show the form inputs.
// Or if the user is logged in successfully,
// You can show the `success` message to him/her,
// Or redirect him/her to another route as mentioned before.
$data['content'] = 'homepage/login';
$this->load->view('templates/no_jsTemplate', $data);
}
如果您将验证规则存储在配置文件application/config/form_validation.php
中,那么 NOT 需要使用set_rules()
方法来设置规则(就像我一样)< / em>的。 但是,Controller/Method
已更改,您应该将Rule Group
名称更改为您的名称。在这种情况下,它将是c_homepage/index
:
$config = array(
'c_homepage/index' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
)
);
当规则组的名称与控制器类/函数相同时 它将在调用run()函数时自动使用 这个类/功能。
- CI User Guide 。
希望它有所帮助。
答案 2 :(得分:0)
您应该将验证规则移至config/form_validation.php
文件。如果您的网站需要使用大量表单,最好自动加载表单验证库。
类Controller扩展CI_Controller { 公共职能指数();
public function method()
{
//If you set the config file to match
//the controller and method name,
//you won't need to add it as a param.
if(!$this->form_validation->run())
{
return $this->index();
//or if your using ajax, you would just send back a status
//and handle errors on frontend
return $this->output->set_status_header(401);//un-authorized
}
//we got this far so validation must have passed :)
}
}
-
$config = array(
'controller/method' => array(
array('field'=>'name', 'label'=>'Name', 'rules'=>'required|xss_clean')),
);