tags:

views:

67

answers:

3

Hi! I am trying to code my first codeigniter project. I have a login controller which basically filters the data inputed and calls a model function that checks if the user is found in the database. What I am trying to do is reuse this controller on the index page. So basically I want to be able to do user login on the index page or on the normal controller page (index.php/login/) without code duplication. I'm sure there is an easy way to do this, but I'm not sure what the best solution is. Make it a library? Thanks!

A: 

You can try creating a form on the index page and submit it to index.php/login/. This way you won't need two entry points.

Sam Dark
+2  A: 

For this I would simply make the form in your view post to the login controller.

As a more generic way to share code and logic throughout your application, take a look at this article:

CodeIgniter Base Classes: Keeping it DRY

You basically give each of your controllers a "type". Being logged in could be a criteria of one of your base controllers, which saves you trying to directly access any of your controllers which is bad mojo.

Phil Sturgeon
awesome article, thanks!
guy
Using the _autoload magic function to connect to the base controller was exactly the bit I was looking for. Good pointer and great article!
JannieT
A: 

Just do the same as you have done for the login View, specify the same action attribute of the form to the index View, and it will be sent to the same login controller with no need to create the two login controllers. You might want to append a query string in the action attribute of the form to distinguish from which View the request has come.

Sarfraz