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 .

  • You can set and get the values by session, do you want use only session ? have you tried Message\ManagerInterface $messageManager Jul 26, 2016 at 17:26
  • i have tried this but no luck.
    – Naveenbos
    Jul 27, 2016 at 5:10
  • do you want use only session ? Jul 27, 2016 at 5:18
  • I am trying to show a success message after submit the form successfully.
    – Naveenbos
    Jul 27, 2016 at 5:20
  • @ Naveen Check my answer added Jul 27, 2016 at 5:46

4 Answers 4


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

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

  • ,How to I add this reference container in backend
    – Jaisa
    Feb 8, 2018 at 6:16
  • Is the XML really needed here?
    – Wouter
    Jun 28, 2018 at 6:44

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

Magento Message Framework class

use \Magento\Framework\Message\ManagerInterface;

use below code in your file to add messages,

protected _messageManager;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context, 
    \Magento\Framework\Message\ManagerInterface $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.

  • which file i want to add this code
    – Naveenbos
    Jul 27, 2016 at 6:03

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 \Magento\Framework\App\Action\Action

    public function __construct(
        \Magento\Framework\App\Action\Context $context
    ) {
    public function execute()

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

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();       
$question = $objectManager->create('Myvendor\Mymodule\Model\Feedback');

/****** 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.

  • 2
    Do not use the Object Manager directly. Nov 12, 2017 at 9:14

Custom layout file

<referenceContainer name="page.messages">
        <block class="Magento\Framework\View\Element\Messages" 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.'));   
  • If you post code as an answer, please at least explain briefly what this code is doing and where it belongs. Answers with code but no explanation are rarely useful. Jun 9, 2017 at 14:54
  • @Jeeva Chezhiyan, I nees to add this in backend , where i need to add the above reference container
    – Jaisa
    Feb 8, 2018 at 6:09
  • i am calling tge controller using ajax, but i don't want to send response success status to js,i just need to show my message using messagemanager, is this possible
    – Jaisa
    Mar 8, 2019 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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