Display notification messages in Magento 2
If you need your module to display messages via Magento notification system, we will show you the example of the code you should use.
If you are using a controller, then most probably you have extended
\Magento\Framework\App\Action\Action
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.') );
In other case
class TonyBlog{/*** @var \Magento\Framework\Message\ManagerInterface*/private $messageManager;public function __construct(\Magento\Framework\Message\ManagerInterface $messageManager){$this->messageManager = $messageManager;}public function tonyFunction(){$this->messageManager->addSuccess('Add your success message');}}
That’s all.
- 1 Magento ver. 2.2.0-dev - A technical prob... 16579
- 2 ECS git pull 代码很慢的问题 14712
- 3 玩客云监工客户端 13202
- 4 Magento 2 UI Form Validation 13119
- 5 Codeigniter SSL 强制重定向至https访问 13004
- 6 magento2 完整卸载模块的正确姿势 12638
- 7 Magento fix 500 12279
- 8 Magento index is locked by another reinde... 10737
- 9 Ubuntu删除日志文件 9009
- 10 Magento 2安装ElasticSearch 6.x搜索引擎-Ub... 8621