Magento2 Notification messages
In your class, inject the Message ManagerInterface class:
__construct(\Magento\Framework\Message\ManagerInterface $messageManager) {
$this->messageManager = $messageManager;
}
Note, Magento has deprecated methods:
$this->messageManager->addSuccess()
$this->messageManager->addError()
$this->messageManager->addWarning()
$this->messageManager->addNotice()
So instead you should use:
$this->messageManager->addSuccessMessage()
$this->messageManager->addErrorMessage()
$this->messageManager->addWarningMessage()
$this->messageManager->addNoticeMessage()
If you are using a controller, then most probably you have extended
\Magento\Framework\App\Action\Action
https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/App/Action/Action.php
This injects the \Magento\Framework\Message\ManagerInterface
object in its __construct
function using the \Magento\Framework\App\Action\Context $context
object
So to display a message,
Success -
$this->messageManager->addSuccess( __('This is your success message.') );
Error -
$this->messageManager->addError( __('This is your error message.') );
Warning -
$this->messageManager->addWarning( __('This is your warning message.') );
Notice
$this->messageManager->addNotice( __('This is your notice message.') );
Inside other classes
class Dummy
{
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
private $messageManager;
public function __construct(\Magento\Framework\Message\ManagerInterface $messageManager)
{
$this->messageManager = $messageManager;
}
public function someFunction()
{
$this->messageManager->addSuccess('Add your success message');
}
}
So as far as i have noticed, Magento2 has removed session class specific notification messages.