[eluser]Unknown[/eluser]
I am learning CI, and I have a doubt, i want to optimizate the code in the login, because i have a lot of pages, where if you are not logged, the login form will appear.
The example of the tutorial:
Controller:
Code:
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
View:
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
This work fine, but i want to optimizate this for multiple page's with a option of login, the tipical .php View:
Code:
<?php
$sid = session_id();
if($sid) {?>
<div class="blabla">
<!-- HTML -->
</div><?php
}
else {?>
<div id="login">
<h3 class="hr_divider">Member Login</h3>
<?php
echo validation_errors();
?>
<?php
echo form_open('form');
?>
<label for="username">Username:</label> <input type="text" name="username" value="" size="50" />
<label for="email">Password:</label> <input type="text" name="password" value="" size="50" />
<div class="cleaner_h10"></div>
<input type="submit" value="Submit" />
</form>
</div><?php
}
?>
I search in forum, and understand the MVC model, i can't call Controllers from controllers or views (or is not right). The option is a library or helper, but i dont know how i can do it. I wanna save the name of variables, and dont put in all controllers this:
Code:
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
Thanks!!
PD: Sorry the english