How to use a session in Magento

I noticed that there is more than one session class in Magento, for example Mage :: getModel ('core / session'), Mage :: getModel ("client / session"), etc. When I want to use a session as storage, which session class should I choose? And why? I'm just confused.

+3
source share
1 answer

Magento code is organized into modules. One of the goals of the module is to provide namespaces. That is, modules allow one group of developers to write code without fear that their variables, objects, etc. Will be accidentally overturned by another group of developers.

Each Magento module can have its own session object. By providing each module with its own session object, Magento helps developers avoid name conflicts in the global PHP session variable. For example, the following code

Mage::getModel('core/session')->setData('foo',$someValue); Mage::getModel('customer/session')->setData('foo',$someOtherValue); 

saves both values ​​in the session, although they have the same key.

Regarding which session class you should choose - if you are writing your own module, you should create your own session class / model, thereby avoiding the above conflicts.

In practice, however, storing things on the kernel / session should not be a problem if you somehow summarize your variables.

 Mage::getModel('core/session')->setData('my_namespace_foo',$someValue); 
+12
source

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


All Articles