0

I have created custom contact form and call in cms page. I submit all form field with controller and add success message after submit and redirect back to cms page but success message not appeared in my cms page.

My Controller.php

<?php

namespace Vendor\Module\Controller;

use Magento\Framework\Controller\ResultFactory;

class Form extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $post = (array) $this->getRequest()->getPost();

        if (!empty($post)) {


            $this->messageManager->addSuccessMessage('My custom Message !'); // This message not appeared in cms page after redirect

            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/mycmspage');

            return $resultRedirect;
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

I also add cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="page.messages">
        <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages"/>
    </referenceContainer>
</page>

Help Appreciated...

3
  • Is that your cache is disabled? try like that if come means i'll tell solution Commented Jan 3, 2018 at 13:45
  • @JeevaChezhiyan cache is enabled
    – Rahul
    Commented Jan 4, 2018 at 4:58
  • Mee to faced this issue i disabled the cache message is shown for me, Please check like that and if that work means need to disable the cache for that particular block Commented Jan 4, 2018 at 9:15

2 Answers 2

1

Update your message code with below line:

 $this->messageManager->addSuccess('My custom Message !');
0
0

Try to use following code

<?php

namespace Vendor\Module\Controller;

use Magento\Framework\Controller\ResultFactory;

class Form extends \Magento\Framework\App\Action\Action
{
    protected $_messageManager;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context, 
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        parent::__construct($context);
        $this->_messageManager = $messageManager;
    }

    public function execute()
    {
        $post = (array) $this->getRequest()->getPost();

        if (!empty($post)) {


            $this->messageManager->addSuccessMessage('My custom Message !'); // This message not appeared in cms page after redirect

            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/mycmspage');

            return $resultRedirect;
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}
2
  • $this->messageManager already exists in the extended class, not need to create constructer. Commented Jan 4, 2018 at 4:47
  • We need not construct messageManager because it's already in extended class as @JigneshKhunt
    – Rahul
    Commented Jan 4, 2018 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.