1

I have created admin grid and associated form,It saved the items and worked good, But failed to show the response message.Please provide me a solution

<?php

namespace X\Y\Controller\Adminhtml\aaa;

use X\Y\Model\ImageFactory;
use Magento\Framework\Message\ManagerInterface;

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

    protected $model;
    protected $_messageManager;

    public function __construct(
    \Magento\Backend\App\Action\Context $context, ImageFactory $model, ManagerInterface $messageManager, \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory, \Magento\Backend\Model\Auth\Session $authSession
    )
    {
        $this->_messageManager = $messageManager;
        $this->model = $model;
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->authSession = $authSession;
        parent::__construct($context);
    }

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

            $formData = $this->getRequest()->getParam('general');
            $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;

            if (isset($formData['image'])) {
                $formData['image'] = $formData['image'][0]['name'];
            } else {
                $formData['image'] = null;
            }

            //Set all the data in DB from form
            $imageData->setData($formData);
            $imageData->save();


           //After saved the item it failed to show the below success message
           $this->_messageManager->addSuccess(__("Your Slide has been saved !"));

        } catch (Exception $e) {

            $this->_messageManager->addError($e->getMessage());
        }
        //Result redirected to index controller     
        return $this->resultRedirectFactory->create()->setPath('a/b/index');
    }

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.