Codeigniter:在将数据传递给controller_2后,将数据从controller_1加载到view_1

时间:2015-06-25 17:35:52

标签: php html codeigniter

这似乎很简单,但找不到答案。我有一个带有数据标题变量的登录控制器:

$data['title'] = 'Login here';
$this->load->view('login/login_view', $data);

使用以下内容正确加载视图:

<title>Login here</title>

酷! 但是当我使用表单登录并将数据发送到“login_verify”控制器时,它会检查是否存在匹配项或是否填充了字段,如果为false则返回原始登录视图:

function index() { 
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');

   if($this->form_validation->run() == FALSE) {
     //Field validation failed.  User redirected to login page
     $this->load->view('login/login_view');
  } else {
    //Go to private area
    redirect('home', 'refresh');
  }
}

如果验证失败,如何将原始$ title变量返回到登录视图?在加载登录视图之前,是否重新启动原始登录控制器?如果是这样,怎么样?显然我不想在“login_verify”控制器中重新定义$ title变量。

由于

1 个答案:

答案 0 :(得分:0)

There's really not a way to do it without either re declaring the title variable, or setting a session up. Instead of $this->load->view('login/login_view'), why not just redirect back to the login screen redirect('login')? Or if you want to get really fancy, you could make that function an ajax function, and pass the form in through javascript instead of having the user reload their page.

On a side note: You really don't need a separate controller for "login_verify", you can do all of that from the original "login" controller and just make a function called "verify", which you can then get to by navigating to "login/verify". You want your controllers to be broad, but specific to a group of actions.