0

我有一个这样的控制器:

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();
            }
        }
    } 

但是当这样做时,这让我陷入了无限循环。我已经构建了其他应用程序,其中我将具有加载视图的功能,然后是控制器将数据发送到模型的功能。我只是想学习其他更好的做事方式。我的成绩反映在这个项目上。我的主要目标是尽可能干燥。我想尝试编写更少的代码以获得更多的成功。希望这不是要求太多。

4

3 回答 3

1

试试这个希望它有效,可能需要进行一些调整,将您的表单提交更改URLindex功能:

已编辑

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)返回所有内容,仅用于检查 post 数组是否有内容。

于 2013-08-13T13:20:49.613 回答
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中,则不需要使用set_rules()方法来设置规则(就像我一样)但是,既然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 用户指南

希望能帮助到你。

于 2013-08-13T14:46:01.710 回答
0

您应该将验证规则移动到config/form_validation.php文件中。如果您的站点需要使用大量表单,最好自动加载表单验证库。

类控制器扩展 CI_Controller { public function index();

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')),
);
于 2013-08-13T18:34:26.930 回答