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.

  • please let me know if you have issue – Rakesh Jesadiya Jan 12 '17 at 6:45
5

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;
            }
  • 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 Jan 12 '17 at 7:01
  • Remove var folder and check now – Rakesh Jesadiya Jan 12 '17 at 7:10
  • @Pinku Have you checked? – Rakesh Jesadiya Jan 12 '17 at 7:20
  • In var lots of folder are there so full folder need to delete or else any specific folder – Pinku Jan 12 '17 at 7:21
  • you can delete full folder – Rakesh Jesadiya Jan 12 '17 at 7:24
2

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.

Your Answer

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

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