How to show session messages at front end in magento 2 beta
I have faced issue related to display message in Magento2 Beta source. In some source by default display error and success message but in some case it do not display then follow below steps.
Step 1: I have created Message.php file in my custom module in Darsh\Crud\Block\Message.php
namespace Darsh\Crud\Block; class Message extends \Magento\Framework\View\Element\Messages { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Message\Factory $messageFactory, \Magento\Framework\Message\CollectionFactory $collectionFactory, \Magento\Framework\Message\ManagerInterface $messageManager, array $data = [] ) { parent::__construct( $context, $messageFactory, $collectionFactory, $messageManager, $data ); } protected function _prepareLayout() { $this->addMessages($this->messageManager->getMessages(true)); return parent::_prepareLayout(); } }
Step 2: Added message block in to handeler in Darsh\Crud\view\frontend\layout\crud_index_form.xml
<referenceContainer name="page.messages">
<block class="Darsh\Crud\Block\Message" name="darsh.crud.message" />
</referenceContainer>
Please add opening and closing Tag <> for layout handle.
Now you can see your custom message added in Darsh\Crud\Controller\Index\Form.
If you face any issue please see Example module on https://github.com/Darshanmodi1427/Magento2_Crud_Operation.git
In layout file i have used the below code. now i got the message in frontend.
<referenceContainer name="page.messages">
<block class="Magento\Framework\View\Element\Template" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
<block class="Magento\Framework\View\Element\Messages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>
</referenceContainer>
I have faced the same issue and I was able to fix it by adding the following in my layout file:
<body>
<!-- your layout content -->
<referenceContainer name="page.messages">
<block class="Magento\Framework\View\Element\Messages" name="messages" as="messages"/>
</referenceContainer>
</body>
This should fix it ;)