[eluser]Glen Swinfield[/eluser]
Ok, this is going to consist of 2 controller, 2 views and a model. I haven't used any CI helpers, I've just coded the form by hand - you use whatever method suits best:
First class is controller - just an example of any controller:
Code:
class front extends Controller
{
/**
* Constructor
*/
function front(){
parent::Controller();
}
/**
* Any old action
*
*/
function aGenericPage(){
// Just some example vars used in the view
$data['meta_title'] = 'The page title';
$data['page_title'] = 'The page title';
$data['content'] = 'Some example content';
// load the view with $data
$this->load->view('generic_page', $data);
}
}
We have loaded the 'generic_page' view, i assume you want a mini login form in this page so the view 'views/generic_page.php' looks like:
Code:
<!-- the html header (removed for quickness -->
<body>
<h1><?php echo $page_title; ?></h1>
<div id="content">
<?php echo $content; ?>
</div> <!-- END CONTENT -->
<div id="sidebar">
<div class="sidebar-module">
<!-- include the form fragment -->
<?php $this->load->view('mini_login'); ?>
</div>
<ul>
<li>Perhaps a list here</li>
<li>Perhaps not</li>
<li>Perhaps a list here</li>
<li>Perhaps not</li>
<li>Perhaps a list here</li>
<li>Perhaps not</li>
</ul>
</div><!-- END SIDEBAR -->
</body>
</html>
You login form may look like: (the redirect input is optional, it's just one way of sending a return page in needed)
Code:
<form action="/auth/login" method="post" id="mini_login_form">
<input type="hidden" value="<?php echo $_SERVER['REQUEST_URI']; ?>" name="redirect" />
<p><label>Username:</label> <input type="text" name="username" size="20" /></p>
<p><label>Password:</label> <input type="password" name="password" size="20" /></p>
<p><input type="hidden" valie="Login" /></p>
</form>
When the login form is submitted it posts the data /auth/login as below:
Code:
class auth extends Controller
{
function auth(){
parent::Controller();
$this->load->database();
$this->load->helper('url');
$this->load->library('validation');
// See further down for an explanation of this model
$this->load->model('authmodel');
}
/**
* this is the action that does the login
*/
function login(){
// Set up validation parameters
$validation = $this->authmodel->validationCriteria();
// Just get the validation criteria from the model
$this->validation->set_rules($validation['rules']);
$this->validation->set_fields($validation['fields']);
$this->validation->set_error_delimiters('<li>', '</li>');
// put your login code here with validation run() etc.
// At this point either user is logged in or not, where you send them is up to you
// i.e. back to previous page, or admin area or whatever.
}
}
I would create a model, perhaps 'authModel' and have the following method in it:
Code:
function validationCriteria(){
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$rules['username'] = 'rules_here';
$rules['password'] = 'rules_here';
return array('fields' => $fields, 'rules' => $rules);
}
This is just for convenience - it gives 1 central place to keep validation rules so you don't have to repeat yourself, also it makes sense to me to keep validation rules in a model - but you could put them elsewhere.
This is just off of the top of my head and hasn't been tested, there are probably better ways - to recap here is what this achieves:
Display a page with a small login form included in the page. When the form is submitted it posts to another controller that loads validation info from a model, validates the request to login and then does whatever you choose - redirect, send to an admin area, log the action, the user just sees a short delay while the form is processed.
Like I said, I haven't tested this but hopefully you get the idea. I'm still not 100% I have understood your question.
Let me know how you get on.