美文网首页
Display notification messages in

Display notification messages in

作者: 葡萄月令 | 来源:发表于2018-05-20 13:51 被阅读16次

    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.


    More Information, Please Subscribe My Wechat Public Platform Or View My Blog : https://www.abmbio.xin

    20180117_81914.jpg

    相关文章

      网友评论

          本文标题:Display notification messages in

          本文链接:https://www.haomeiwen.com/subject/xaftjftx.html