3

What does this function do in magento?

$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');

What _initLayoutMessages() return?

2 Answers 2

8

The method returns nothing. Well, actually it returns $this.
From different parts of the application (mostly controllers) you can set session messages that are displayed in the next page you visit.
Here is an example.
When subscribing to a newsletter, when the page refreshes you will see a message Thank you for your subscription..
This message is added from Mage_Newsletter_SubscriberController::newAction by this line:

$session->addSuccess($this->__('Thank you for your subscription.'));

Where $session = Mage::getSingleton('core/session').

By calling $this->_initLayoutMessages('customer/session'); inside an action you tell magento to show all session messages that were previously added by using:

Mage::getSingleton('customer/session')->addError(...) (or addWarning, addNotice and addSuccess).

Since the messages can be added by using different session parts (customer/session, catalog/session, checkout/session, core/session) this allows you to separate and control the messages that the user sees.

For example if on a previous action you added a message like this:

Mage::getSingleton('customer/session')->addError('Some error here');  

the user won't see this message until he visits a page that maps to an action that has $this->_initLayoutMessages('customer/session');.

0
4

The method _initLayoutMessages adds a message block to the page layout that outputs any error, notice or success messages stored in the session model stated as argument.

So $this->_initLayoutMessages('catalog/session'); will output a message added via

Mage::getSingleton('catalog/session')->addError('foo');
Mage::getSingleton('catalog/session')->addSuccess('foo');
Mage::getSingleton('catalog/session')->addNotice('foo');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.