Welcome Guest, Not a member yet? Register   Sign In
What goes in model, controller or library?
#1

[eluser]LAMP Coder[/eluser]
I know there is no hard thumb rule about this, but following is what I think is right:

Model:
Everything that talks to database

Controller:
- This is the bridge between Model and View
- Interacts with models, processes the data received from models and passes the data to views

View:
Everything that is seen by the user

Now, here is my question, let's say we have a Users model and I want to login the user.
- One idea is to limit models to only talk to database and do nothing else. i.e. read, write, update and add. hence model will return the data to controller and that will do the work for authenticating and settings the session data
- Or I can add another method to model called "authenticate" and that will do the authentication, setting the session. This will mean I will need another method called "check_login" under the same model.

Which one should I use? What are your views on this? What goes in libraries, is there any rule for that?
#2

[eluser]mddd[/eluser]
Models:
Many people (especially when just starting out) seem to think that the model is only for talking to the database. I disagree. The model is the place for the 'smarts' of your application. It can contain much more than just getting and putting stuff in the database. If you are doing any kind of work on your data (reorganizing, editing, whatever) I say: do it in the model.

Controllers:
Keep them as simple as possible. Their job is to control, as the name implies. Only process data in the controller if it has to do with controlling the flow of the application. Validation is such a case: you need to validate data, and depending on the outcome, take some action. I think it's okay to do that in the controller, but if there is more complex data handling involved, I say: do it in the model.

Views:
Keep them as html-only as possible. Of course there is php for putting in your variables, and for control structures (if, foreach etc.). The view should only be "showing" stuff, not "doing" anthing.
I'm not sure what you mean by 'is sent to the user's database.. can you clarify?

On your question: I would choose your second option. Especially because authentication is going to happen in multiple places. Keep the code centralized and just call it from your controllers.

On libraries: if a certain section of code is pretty much independent, put it in a library. Libraries are meant to be re-used in other projects. That's the main difference between a library and a model. The model is very much related to your site. A library is something that could be useful in other sites (by other people) and should be as independent as possible (for instance not calling directly on models).
#3

[eluser]LAMP Coder[/eluser]
I meant "sent to the user", I had two thoughts in my mind and they got mixed up while typing.. that happens to me sometimes.
#4

[eluser]mddd[/eluser]
Smile No problem!!
#5

[eluser]wiredesignz[/eluser]
http://www.phpwact.org/pattern/model_view_controller
#6

[eluser]LAMP Coder[/eluser]
[quote author="wiredesignz" date="1281714469"]http://www.phpwact.org/pattern/model_view_controller[/quote]

bookmarked, will look at it later.. thanks




Theme © iAndrew 2016 - Forum software by © MyBB