How to show success message in session Magento2

The question:

In magento 1 i can do it like this

$session            = Mage::getSingleton('core/session');
$session->addSuccess($this->__('Your Request has been received. One of our sales representatives will contact you shortly.'));

Same thing how can i do it in Magento2? Tried this but not working .

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

In Magento 2, we can do achieve this in two steps:

First, we need to assign the message to ‘messageManager’ from your module as below:

$this->messageManager->addSuccess(__('This is a success message.'));

Secondly, we need to assign a placeholder for the message through frontend layout xml used in the module as given below inside the <body> tag:

<referenceContainer name="page.messages">
        <block class="MagentoFrameworkViewElementTemplate" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
        <block class="MagentoFrameworkViewElementMessages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>

The above layout update will make use of the magento’s message template to display the messages.

Method 2

Magento2 is using MessageInterface to add all message, please use below code to show Messages

Magento Message Framework class

use MagentoFrameworkMessageManagerInterface;

use below code in your file to add messages,

protected _messageManager;

public function __construct(
    MagentoFrameworkViewElementTemplateContext $context, 
    MagentoFrameworkMessageManagerInterface $messageManager
) {
    $this->_messageManager = $messageManager;

and then add below functions in your methods to show messages:

$this->_messageManager->addError(__("Error Message"));
$this->_messageManager->addSuccess(__("Success Message"));

I hope this will help you fixing your issue.

Method 3

You can try below code to add success or error Messages.

$this->messageManager was in parent class calling from


$this->messageManager = $context->getMessageManager();

class Post extends MagentoFrameworkAppActionAction

    public function __construct(
        MagentoFrameworkAppActionContext $context
    ) {
    public function execute()

$data = $this->getRequest()->getPostValue();

$objectManager = MagentoFrameworkAppObjectManager::getInstance();       
$question = $objectManager->create('MyvendorMymoduleModelFeedback');

/****** set your data *********/

$this->messageManager->addSuccess( __('Thanks for your valuable feedback.') );

/* ***** OR

$this->messageManager->addError('There is something went wrong');

You can assign messages to messageManager


You can find some more information on how to Display notification messages

Hope this helps.

Method 4

Custom layout file

<referenceContainer name="page.messages">
        <block class="MagentoFrameworkViewElementMessages" name="messages" as="messages"/>

Use this in your custom layout file to show the Error message and Success message for your custom template file.

Custom Controller file

                    __('Thanks for Submission'));
    }catch (Exception $e) {
                    __('We can't process your request right now. Sorry, that's all we know.'));   

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment