Show Success, Error, Warning messages in Magento 2

To be able to output warnings, error and notice messages in our modules we have to use the following interface \Magento\Framework\Message\ManagerInterface.

The best way to use it is through Dependency Injection in the constructor as follows:

namespace VendorName\ModuleName\Block;

class MyClass
    public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->_messageManager = $messageManager;

Once this interface is added and initialized in the constructor, it becomes available to be used anywhere in the class, and we could use any of the following methods:

// Deprecated Methods

Important: Magento has deprecated these methods in commit cd13a85f 18 May 2016

Instead, we should use the following methods:

// New Methods


$this->messageManager->addWrningMessage('You need to choose options for your item.')


Next Post Previous Post