Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework application design - should session variables be accessed in the Model layer

I am working on this app that accesses session variables in the model layer. This just seems wrong but am willing to be proven wrong. Maybe not wrong but, in most places in app, session variables are handled in controller and passed in as arguments but, in other places, the session value is just accessed. Am I wrong that this seems like bad practice?

edit: one reason I don't like sessions in models is that it seems to make it more complex to test. Keep it as just params passsed to functions and then recordset passed back.

thx

like image 802
timpone Avatar asked Sep 02 '11 19:09

timpone


2 Answers

It depends.

The way I think about this is such:

  1. A Model represents your data layer.
  2. most of the time that data layer will be DB Table based
  3. The Session is just another data storage medium.
  4. Conclusion: If the data that your model represents is stored in the Session, than it is OK to access that data from within the model

An example is a Session based shopping cart. My cart's objects are models of my session data.

like image 117
Francis Yaconiello Avatar answered Oct 22 '22 17:10

Francis Yaconiello


Controller shd do a check weather session exist or not before using the model which uses that session inside it .

like image 43
Mr Coder Avatar answered Oct 22 '22 19:10

Mr Coder