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"/>
</referenceContainer>

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
) {
    parent::__construct($context);
    $this->_messageManager = $messageManager;
}

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

$this->_messageManager->addError(__("Error Message"));
$this->_messageManager->addWarning(__("Warning"));
$this->_messageManager->addNotice(__("Notice"));
$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

MagentoFrameworkAppActionAction

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

class Post extends MagentoFrameworkAppActionAction
{

    public function __construct(
        MagentoFrameworkAppActionContext $context
    ) {
        parent::__construct($context);
    }
    public function execute()
    {

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

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

/****** set your data *********/
$question->setData($data);
$question->save();

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

/* ***** OR

$this->messageManager->addError('There is something went wrong');
$this->_redirect('*/');
return;
    }

You can assign messages to messageManager

$this->_messageManager->addError(__("Error"));
$this->_messageManager->addWarning(__("Warning"));
$this->_messageManager->addNotice(__("Notice"));
$this->_messageManager->addSuccess(__("Success"));

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"/>
 </referenceContainer>

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

Custom Controller file

 try{
        $school_model->save();
        $this->messageManager->addSuccess(
                    __('Thanks for Submission'));
    }catch (Exception $e) {
        $this->messageManager->addError(
                    __('We can't process your request right now. Sorry, that's all we know.'));   
    }
    $this->_redirect('*/*/');
    return;


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

Leave a Comment