0

I use magento 2.2.1 version. I just tried to show notification messages and I use this code.

You can go to Controller/Test/Crud.php. I also injected \Magento\Framework\Message\ManagerInterface $messageManager. But I couldn't see messages.

What is wrong in this code or what I need to show the messages ?

class Crud extends \Foggyline\Office\Controller\Test
{
    protected $resultPageFactory;
    protected $_messageManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->_messageManager = $messageManager;


        return parent::__construct($context);
    }

    /**
     * Url like http://magento2.ce/index.php/foggyline_office/test/crud/
     */
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $this->_messageManager->addSuccess('Success-1');
        $this->_messageManager->addSuccess('Success-2');
        $this->_messageManager->addNotice('Notice-1');
        $this->_messageManager->addNotice('Notice-2');
        $this->_messageManager->addWarning('Warning-1');
        $this->_messageManager->addWarning('Warning-2');
        $this->_messageManager->addError('Error-1');
        $this->_messageManager->addError('Error-2');

        return $resultPage;
    }
}
  • Did you check by extending \Magento\Framework\App\Action\Action in your controller instead of \Foggyline\Office\Controller\Test ? – Jaimin Sutariya Nov 16 '17 at 13:07
  • ok I will try now – Oğuz Can Sertel Nov 16 '17 at 13:24
  • it doesn't run. Can anyone clone that git and try in local enviroment? – Oğuz Can Sertel Nov 16 '17 at 13:34
  • I have just installed the module on Magento 2.2.0 and I am able to see the messages after an alert box on the page. So your code is correct there is something else in your setup which is causing the issue. – Jaimin Sutariya Nov 16 '17 at 13:38
0

You need inject the message manager object into the constructor.

class Example {

protected $_messageManager;

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

public function method( ) {
    ..
    $message = '...';
    $this->_messageManager->addError($message);
    ..
}

}

Check this link below

How to use messageManager to show an error after redirect

  • I tried this already. Still not working. – Oğuz Can Sertel Nov 16 '17 at 6:04
  • Did you inject message manager interface into your constructor!. Paste your new codes. It should work. You can not directly use messageManager. Your codes are wrong. You must write $this->messageManager->addSuccessMessage('Success-1'); into execute() method of your controller class. Check properly please – Goutam Nov 16 '17 at 6:27
  • yes of course I have injected. not working. I referenced this code github.com/ajzele/B05032-Foggyline_Office and injected message manager interface. – Oğuz Can Sertel Nov 16 '17 at 6:40
  • could you clone the module to test it ? – Oğuz Can Sertel Nov 16 '17 at 6:44
  • I checked your Crud.php and did not find anything that you learned from the answer. Create a protected $_messageManager; Pass \Magento\Framework\Message\ManagerInterface $messageManager into your constructor. Initialize $this->_messageManager = $messageManager; into public function __construct and then use $this->_messageManager->addError($message); into your execute method – Goutam Nov 16 '17 at 6:48

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.