Should you process session data in a controller or model for an MVC structure?

I work with MVC infrastructure (in particular, PHP CodeIgniter ). I try to adhere to the "best practices" as much as possible, but I do not have much experience with MVC. Is it bad practice to retrieve data from a session in a controller? Should I β€œask” the model if, say, the specific session value is β€œfoo” or β€œbar”, or should I do this directly inside the controller? I think I should do this inside the Model, since the session data is technically data, but I just want to make sure. To be clear, I don't store session data in the database at all, so I don't run the query.

+4
source share
1 answer

Models usually process all domain objects that are stored in some kind of long-term storage. They may or may not have transitional meanings in them that are related to their specific application.

Controllers must request any data necessary for proper routing and display of information. This can help create a β€œService” layer that works directly on domain objects (your model) and provides an API for controllers. The main thing is not to include business logic in the Controllers.

It would be reasonable, for example, for the Controller to capture the link page and do something with this data in relation to the user stream. However, in addition to checking, he probably should not, say, check the amount of money transferred between accounts - it should simply pass this to the service object that creates the instances and works with the correct domain objects.

Questions about what logic you enter into the controller:

  • Does this logic help me figure out which model objects I need? If not, it should not be here.
  • Does this logic help figure out which View objects will be used to build the response to the user? If not, it should not be here.
+3
source

All Articles