1

This seems simply but can't find an answer. I have a login controller with a data title variable:

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

The view is loaded correctly with the:

<title>Login here</title>

Cool! But when I login using the form and send the data to the "login_verify" controller it checks to see if there is a match or if the fields were populated and if false it returns to the original login view:

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

How do I get the original $title variable back into the login view if the validation fails? Do I redeclare the original login controller before I load the login view? If so, how? Obviously I don't want to have to redefine the $title variable in the "login_verify" controller.

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Chad Priddle
  • 650
  • 2
  • 15
  • 32

1 Answers1

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.

acupofjose
  • 2,159
  • 1
  • 22
  • 40
  • 1
    Good point. This is day 2 of diving into a new framework and just trying to wrap my head around the MVC. I ended up adding a function in the main controller and then just declaring the variables for the login view at the top of the login.php script – Chad Priddle Jun 25 '15 at 18:36
  • Haha well Codeigniter is a good one! It's a bit difficult to wrap your head around MVC at first, but you'll learn to love it. Let me know if you have more questions! – acupofjose Jun 25 '15 at 18:39
  • if I use redirect then I lose the validation errors correct? – Chad Priddle Jun 29 '15 at 23:47