Alexandru Lighezan
Alexandru Lighezan

Reputation: 112

Codeigniter - reusing controllers?

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!

Upvotes: 1

Views: 440

Answers (3)

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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.

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382646

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.

Upvotes: 0

Sam Dark
Sam Dark

Reputation: 5291

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.

Upvotes: 0

Related Questions