How to show session messages at front end in magento 2 beta

The question:

I want to show an error or success message in my magento 2 module. I have extended my front-end controller with the class name MagentoFrameworkAppActionAction. Then I redirect client to home page using following code

$resultRedirect = $this->resultRedirectFactory->create();

Everything works fine but this line never prints the error


The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I have faced issue related to display message in Magento2 Beta source. In some source by default display error and success message but in some case it do not display then follow below steps.

Step 1: I have created Message.php file in my custom module in DarshCrudBlockMessage.php

namespace DarshCrudBlock;

class Message extends MagentoFrameworkViewElementMessages {

    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoFrameworkMessageFactory $messageFactory,
        MagentoFrameworkMessageCollectionFactory $collectionFactory,
        MagentoFrameworkMessageManagerInterface $messageManager,       
        array $data = []
    ) {

    protected function _prepareLayout()
        return parent::_prepareLayout();


Step 2: Added message block in to handeler in DarshCrudviewfrontendlayoutcrud_index_form.xml

   <referenceContainer name="page.messages">
        <block class="DarshCrudBlockMessage" name="darsh.crud.message" />

Please add opening and closing Tag <> for layout handle.

Now you can see your custom message added in DarshCrudControllerIndexForm.

If you face any issue please see Example module on

Method 2

In layout file i have used the below code. now i got the message in frontend.

<referenceContainer name="page.messages">            
    <block class="MagentoFrameworkViewElementTemplate" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
    <block class="MagentoFrameworkViewElementMessages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>

Method 3

I have faced the same issue and I was able to fix it by adding the following in my layout file:

    <!-- your layout content -->
    <referenceContainer name="page.messages">
        <block class="MagentoFrameworkViewElementMessages" name="messages" as="messages"/>

This should fix it 😉

Method 4

Have you tried returning your last line of the code?

return $resultRedirect->setPath($base_path);

The actual addError function looks fine unless the $SomeMessage variable is empty, have you checked that as well?

If that fails maybe you can try redirecting using the _redirect function instead like:


Maybe the resultRedirect function clears the message session so your message never gets displayed.

Method 5

Yes, I have added message using $resultRedirect object with


Please see below controller code

namespace DarshCrudControllerIndex;

use MagentoFrameworkAppFilesystemDirectoryList;

class Post extends MagentoContactControllerIndex {
//class Post extends MagentoFrameworkAppActionAction {

    public function execute() {
        $data = $this->getRequest()->getPostValue();

        if (!$data) {

        $resultRedirect = $this->resultRedirectFactory->create();
        try {
            $model = $this->_objectManager->create('DarshCrudModelCrud');
            if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
                try {
                    $uploader = $this->_objectManager->create('MagentoMediaStorageModelFileUploader', array('fileId' => 'image'));
                    $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                    $mediaDirectory = $this->_objectManager->get('MagentoFrameworkFilesystem')
                    $config = $this->_objectManager->get('DarshCrudModelCrud');
                    $result = $uploader->save($mediaDirectory->getAbsolutePath('bannerslider/images'));
                    $data['image'] = $result['file'];
                } catch (Exception $e) {
                    $data['image'] = $_FILES['image']['name'];
            } else if (isset($data['image']['delete'])) {
                $data['image'] = '';
            } else if (isset($data['image']['value'])) {
                $data['image'] = $data['image']['value'];

            try {
                        __('Thanks for contacting us with your comments and questions. We'll respond to you very soon.')
            } catch (Exception $e) {
                        __('We can't process your request right now. Sorry, that's all we know.'));
        } catch (Exception $e) {
                    __('We can't process your request right now. Sorry, that's all we know.')


        return $resultRedirect;



Method 6

Try using XDEBUG, set break point at your controller action. Maybe it was duplicated redirect and making lost messages in manager.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment