Magento – Success message not shown after saving a item in magento 2

gridmagento2.2.2success-messageuicomponent

I have created a custom admin grid and an associated form in magento 2. While saving a item the success message is not displayed. Do I need any special component in grid to add success message in my save controller Or do I need to inject message manager interface. If this shouldn't be happening please provide me with a solution, thanks
Save.php

<?php



use X\Y\Model\BannerFactory;

class Save extends \Magento\Backend\App\Action
{

    protected $_model;
    protected $_resultRedirectFactory;
    protected $_authSession;
    public function __construct(
    \Magento\Backend\App\Action\Context $context, BannerFactory $model, \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory, \Magento\Backend\Model\Auth\Session $authSession
    )
    {
        $this->_model = $model;
        $this->_resultRedirectFactory = $resultRedirectFactory;
        $this->_authSession = $authSession;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        try {
            $modelData = $this->_model->create();
            //If gets id loads the model
            if ($id) {
                $modelData = $modelData->load($id);
            }

            $formData = $this->getRequest()->getParam('general');
            $imageData = $this->getRequest()->getParam('upload');

            $user = $this->getCurrentUser();

            if (empty($formData ['id'])) {

                $formData['id'] = null;
                // update the current user name for both created and updated
                $formData['created_by'] = $user;
                $formData['updated_by'] = $user;
            }

            $formData['updated_by'] = $user;
            //For small image
            if (isset($imageData['small_image'])) {
                $formData['small_image'] = $imageData['small_image'][0]['name'];
            }

            //Set all the data in DB from form
            $modelData->setData($formData);
            $modelData->save();
            $this->messageManager->addSuccess(__("Your Slide has been saved !"));
        } catch (Exception $e) {
            $this->messageManager->addError($e->getMessage());
        }
        //Result redirected to index controller     
        $resultRedirect = $this->_resultRedirectFactory->create()->setPath('*/*/index');
        return $resultRedirect;
    }

    public function getCurrentUser()
    { //Get the current user name using session
        return $this->_authSession->getUser()->getUsername();
    }

}

Best Answer

If your controller extends \Magento\Backend\App\Action I believe this should work, before doing the redirect

$this->messageManager->addSuccess(__('Your success message here'));

Or...

$this->getMessageManager()->addSuccess(__('Your success message here'));

Update: It could be a layout problem then. More info https://magento.stackexchange.com/a/127689/3566

Related Topic