6

I want call block methods from controller. My files are below

Controller

namespace Vendor\ModuleName\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
{
   public function __construct(Context $context) {
        parent::__construct($context);
    }

    public function execute() {
   }
}

Layout

cms_index_index

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="2columns-left"> 
     <head>
    </head>
    <body>
        <referenceContainer name="content">
            <block   class="Vendor\ModuleName\Block\ProductTabs" name="producttabs" template="producttabs.phtml" ifconfig="form_section/form_group_general/status">
            </block>
        </referenceContainer>
    </body>
</page>

Block

namespace Vendor\ModuleName\Block;
class ProductTabs extends \Magento\Framework\View\Element\Template
{
    public function __construct(\Magento\Framework\View\Element\Template\Context $context,array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getMsg(){
      return 'i am in block';
    }
}

I want getMsg() to be accessed in controller. If anybody know then please share your experience.

1
  • please let me know if you have issue Commented Jan 12, 2017 at 6:45

2 Answers 2

6

You can call directly in controller like this, In your controller file,

 public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
    ) {
        parent::__construct($context);
        $this->_resultLayoutFactory = $resultLayoutFactory;
    }

      public function execute()
            {
                $resultLayout = $this->_resultLayoutFactory->create();
                $resultLayout->getLayout()->getBlock('producttabs')->getMsg()
                return $resultLayout;
            }
9
  • Thanks for reply. I used your way but getting error and one more thing, I want getMsg() to be called from controller and you are calling toHtml(). Can you call getMsg() and reply me. Now some part of error message what i am getting is Fatal error: Uncaught TypeError: Argument 2 passed to Vendor\ModuleName\Controller\Index\Index::__construct() must be an instance of Magento\Framework\View\Result\LayoutFactory, none given
    – Pinku
    Commented Jan 12, 2017 at 7:01
  • Remove var folder and check now Commented Jan 12, 2017 at 7:10
  • @Pinku Have you checked? Commented Jan 12, 2017 at 7:20
  • In var lots of folder are there so full folder need to delete or else any specific folder
    – Pinku
    Commented Jan 12, 2017 at 7:21
  • you can delete full folder Commented Jan 12, 2017 at 7:24
3

You can access your block method like this:

<?php

namespace Vendor\ModuleName\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;


class Index extends Action
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
    ) {
        parent::__construct($context);
        $this->_resultLayoutFactory = $resultLayoutFactory;
    }

    public function execute()
    {
        $resultLayout = $this->_resultLayoutFactory->create();
        $block = $resultLayout->getLayout()->getBlock('producttabs');
        if ($block) {
            $msg = $block->getMsg();
        }

    }
}

Here $msg will hold getMsg() output.

1
  • 1
    Not working.... Commented Jun 13, 2018 at 10:07

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.