Zend Framework Application Development - When accessing session variables in a model layer

I am working on this application that accesses session variables in a model layer. It just seems wrong, but I'm ready to make a mistake. Perhaps this is not so, but in most places in the application, session variables are processed in the controller and passed as arguments, but in other places the session is accessed only through this. Am I mistaken that this sounds like bad practice?

edit: one of the reasons I don’t like sessions in models is because it seems to be harder to check. Store it as soon as the parameters have passed to the functions, and then the recordset has been transferred again.

THX

+6
source share
4 answers

It depends.

The way I think about it is this:

  • The model represents your data layer.
  • in most cases, when a database-based table is displayed at the data level
  • A session is another storage medium.
  • Conclusion If the data represented by your model is stored in a session, then it is normal to access this data from the model

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

+4
source

The shd controller runs a weather check session or not before using a model that uses this session inside it.

+2
source

What is stored in session variables? If it simply 'logged in? Y / N' then they probably shouldn't be part of the model layer. If, however, it’s more complicated, they are probably inextricably linked to your business model and should be treated as such.

The examples at the bottom of the Zend Test documentation show how to test full MVC using the login function. Presumably, could you do the same when testing models?

0
source

No, this should not be. The storage type should be separate from your business logic. For instance:

I have one simple plugin that performs an access check and puts the user object in the registry. Thus, instead of an access session, the model has access to the registry, which is well defined.

$User = Zend_Registry::get('User'); // User model object

From a theoretical point of view, everything should be accessible through data cards. In the future, if you move from the session store to another, you will only need to update it in one place. Your models do not need to know where the data came from.

If you use several ways to get your data, it may cause some problems when your application becomes large.

A proposal for an approach to OOP and multilevel systems is to create specialized objects and layers and simplify simple actions that prevent the spread of certain actions throughout the code.

But then again, you don’t need to change this unless you see the benefits. Keep in mind that sometimes refactoring is more effective than trying to predict everything.

0
source

Source: https://habr.com/ru/post/896520/


All Articles