2

this is rather a conceptual question. I am developing a registration + login system in Codeigniter.

Currently i have a user controller, which would take care of the user's profile and post's related to the user. Well i am confused if I should include the registration and login function inside the user controller or should i have them as standalone controllers with their associated model and view ofcourse?

There is no code to show here, just wanted to know the best practice.

tereško
  • 57,247
  • 24
  • 95
  • 149
AdamShep
  • 31
  • 3
  • Depends upon you and your preference. It's logical in a way to use a different controller as it will show a separate `url` to the user like `site.com/login` and a related model. – Nil'z Aug 08 '13 at 06:40
  • there is a standard, use libraries that are already build (why developing things that are already there for free), http://stackoverflow.com/questions/346980/how-should-i-choose-an-authentication-library-for-codeigniter – Kyslik Aug 08 '13 at 07:00

3 Answers3

0

There is no best practice, just do where you believe in. There are no standards for things like that.

I believe in seperated components so i should create standalone controllers.

Moe
  • 130
  • 5
0

There is no rule that dictates how you must organize your code. However here is what i consider "best practice":

As far as possible, one controller should 'map' to one page.

So 'user page' should have a 'user controller', 'sign in page' a 'sign in controller', 'registration page' a 'registration controller', and so on...

Nothing is stopping us from using one single php files with 10,000 lines for our program, but i think most would agree that the problem with doing that is that maintaining such a code base which be a nightmare.

When you later decide to change the registration page like add a new field, do you want to have to look at a file that contains the user and sign in code logic that you aren't interested in or a file with only what you are concerned with?

What if you had to tell another developer to do it? image this... "help me add a field to the registration form, you will need to look in the user controller and scroll down to the 3 function that is the registration form logic" vs. "help me add a field to the registration form, just look at the registration controller"

I leave you with a quote by Martin Golding

Always code as if the guy who ends up maintaining your code will be a violent psychopath

PK.
  • 2,261
  • 3
  • 23
  • 30
0

I agree with other's answers, there is no standard, you have to follow your personal standard, at your liking. Usually i create a single controller called Auth.php Don't know if it's the best practice but as you can see it will be readable :

class Auth{

function signup(){

}
function login(){

}
function logout(){

}
function recover_password(){

}
}
itsme
  • 47,321
  • 93
  • 214
  • 341