Welcome Guest, Not a member yet? Register   Sign In
Logic of controllers and how to seperate them
#1

[eluser]UnforgiveNX[/eluser]
Hi all,

I'm a bit confused about the controller structure. I'll give an example to my issue.

Say that I'm building a community site. A user comes and logs in. Users can send messages to each other. Now;

I have a auth controller which has "login, logout, register, forget_pass etc." functions.

When it comes to messaging, should I have a seperate controller as "message" which has "send_message, list_messages etc." methods, or should I use the previous "auth" controller to manage the messages? I can do this in both ways as obvious.

How should I decide about creating a controller?

Thanks
#2

[eluser]n0xie[/eluser]
Well you said it yourself: the auth controller has everything to do with users logging in, logging out and retrieving their password.

So this is a good example of bundling up related functionality into one controller.

Now I ask you, what does messaging have to do user authentication? In my book, not much. So I would bundle all matters relating to messaging into one controller.
#3

[eluser]nzmike[/eluser]
By looking at the name of a controller(or any class), you should have a fair idea of its behaviours. It makes sense for login, logout, register, forget_pass to be in Auth as they are all related to the authentication of a user. So it would make sense to separate messages into its own controller.
#4

[eluser]BrianDHall[/eluser]
Basically everything said here already is correct. I would only add that its entirely a matter of how your program works. So far I've only ever used one controller, simply because I don't really have very many 'pages' - usually only about 10-30 and they are generally lightweight, with most of the lines of code being in model functions and controllers. Just haven't needed to split things up.

However as an application grows in features and becomes more complex, you'll want to be able to look at the name of the controller class and go 'ah yes, that must be where that function is'...so if you want a message function you would expect to look in some sort of message-sounding class, etc.
#5

[eluser]UnforgiveNX[/eluser]
Thank you all for your nice replies Smile

It's more clear to me now




Theme © iAndrew 2016 - Forum software by © MyBB