4

I read some questions related to what I'm going to ask but I couldn't really figure it out. Let's revision together:

  • Models are classes that manage datas from and to database. So they are classes database-related.
  • View is the HTML part. Basically the layout of the page.
  • Controller is the logic part of the application and uses models, gets/give input/output from/to the view.

Wonderful. What if I have to use a class that is not database related. Like a class for Sessions, a Template class or a pure logic Class that help me with some common logic function I use. Where should this classes go? Are they Model or just Library classes?

Shoe
  • 74,840
  • 36
  • 166
  • 272

2 Answers2

1

Most MVC frameworks support libraries and helpers. Your general classes can go in those directories.

http://codeigniter.com/user_guide/general/helpers.html

http://codeigniter.com/user_guide/general/creating_libraries.html

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
0

You put it on the model class. Model handles data logic and validations too.

Controllers only handles user input.

Views display what the user sees.

Neigyl R. Noval
  • 6,018
  • 4
  • 27
  • 45
  • 1
    What if I have a class that just don't manipulate datas from and to the database. What if I just have a class that make me easier and cleaner to load application files with... where should it goes? – Shoe Nov 14 '11 at 16:34
  • Model does not only manipulate data to and from the database. See http://stackoverflow.com/questions/8097893/where-to-put-validation-logic-in-mvc-software-architecture for a somehow related question. – Neigyl R. Noval Nov 14 '11 at 16:38
  • 1
    @downvoters: it is perfectly fine to not only have models for accesing database stuff. @ Charliepiga: However the things OP wants to place (common functions / Session class etc) would suit better in another place IMHO for e.g. a folder `/library/` since it will contain (generic) code which could also be used by other projects without changing the code. If that makes sense :) – PeeHaa Nov 14 '11 at 16:41
  • @PeeHaa, Ok so I should create a library/ folder for classes that are not using datas from and to the database. What about classes that receive, validate a output data from the user in put instead? (such as forms) – Shoe Nov 14 '11 at 16:47
  • In that case, that would be in the model class even though no database-related.. What I am actually referring is a strict implementation of MVC software architecture. Most php frameworks have classes that support helper classes that may not be part of m, v, or c. But in the end, if you are strict of mvc, you need to look on how such class is intended. – Neigyl R. Noval Nov 14 '11 at 16:50
  • 1
    @Charliepiga: it is not a matter of whether code will access data from database but rather a matter of will I be using ( / can I use) this code in other projects. This way you can have multiple projects which use the same library (with your common functions). So if you change the library (e.g. bugfix) it changes for all your projects. If you have logic code which you don't need in other projects (whether it accesses the db or not) I would put in in a model. E.g. in my framework I have a base class for forms (with standard validation) and I also have a forms models in my project which extends – PeeHaa Nov 14 '11 at 16:52
  • the base class from the library which does some project specific validation of fields. But that's just how I do it. Might not be the right way™ but the way which works for me. And that's important I think. :D – PeeHaa Nov 14 '11 at 16:53