8

I want getBlock Adb/Block/Index in controller Adb/Controller/Category/View. So How do it?

Edit

in controller.

namespace Addon\Faq\Controller\Category;


class View extends \Addon\Faq\Controller\Category
{
    /**
     * @var \Magento\Framework\Controller\Result\ForwardFactory
     */
    protected $_coreRegistry = null;
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory ;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context, 
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Registry $registry

    ) {
        $this->resultPageFactory  = $resultPageFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context);
    }

    /**
     * Product list page
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {   
        $resultPage = $this->resultPageFactory ->create();
        $blockInstance = $resultPage->getLayout()->getBlock('category.index');

    }
}

layout in frontend

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd"> 
    <body>
        <referenceContainer name="content">
            <block class="Addon\Faq\Block\Faq" name="category.index" template="Addon_Faq::view.phtml"/>               
        </referenceContainer>
    </body>
</page> 

It not call block Faq

1
  • 1
    You should not call blocks in controller. Why do you need it?
    – KAndy
    Commented Oct 26, 2015 at 16:36

5 Answers 5

5

Your return nothing in your execute command. Return resultpage in your controller

    $resultPage = $this->resultPageFactory->create();

    return $resultPage;

your xml name should be faq_category_view.xml.

21

If you want to instantiate a block you need to add to inject in the constructor an instance of \Magento\Framework\View\LayoutFactory

...
protected $layoutFactory;
...
public function __construct(
   ...
   \Magento\Framework\View\LayoutFactory $layoutFactory,
   ...
) {
   ...
   $this->layoutFactory = $layoutFactory;
   ...
}

Then you can instantiate a block like this:

$this->layoutFactory->create()->createBlock('Block\Class\Here');

if you want to access a block defined in the layout you need to inject in the constructor and instance of \Magento\Framework\View\Result\PageFactory

...
protected $resultPageFactory;
...
public function __construct(
   ...
   \Magento\Framework\View\Result\PageFactory $resultPageFactory,
   ...
) {
   ...
   $this->resultPageFactory = $resultPageFactory;
   ...
}

Then you will be able to access the block like this:

$resultPage = $this->resultPageFactory->create();
$blockInstance = $resultPage->getLayout()->getBlock('block.name.here');
1
  • is this still the recommended way of calling the block? Can I not inject? :)
    – treyBake
    Commented Dec 18, 2018 at 16:43
9
  1. Create the block:

     <?php
        namespace Training\Test\Block;
        class Test extends \Magento\Framework\View\Element\AbstractBlock
        {
           protected function _toHtml() {
             return "<b>Hello world from block!</b>";
           }
        }
    
  2. Create an action class:

    <?php
    namespace Training\Test\Controller\Block;
    class Index extends \Magento\Framework\App\Action\Action
       {
       public function execute() {
         $layout = $this->_view->getLayout();
         $block = $layout->createBlock('Training\Test\Block\Test');
         $this->getResponse()->appendBody($block->toHtml());
       }
    }
    
2
  • 3
    Do not use $this->_view in controllers. I read it is @deprecated. Use resultPage->getLayout() instead like the other answer suggests.
    – 7ochem
    Commented Nov 21, 2016 at 10:57
  • Looks More Comprehensive
    – Anees
    Commented Dec 29, 2020 at 14:01
5

See the core code:

Step 1

https://github.com/magento/magento2/blob/02e0378c33054acb0cdb8d731d1e2b2c2069bc1b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php#L26-L34

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

Step 2

https://github.com/magento/magento2/blob/02e0378c33054acb0cdb8d731d1e2b2c2069bc1b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php#L69-L70

/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();

Step 3

https://github.com/magento/magento2/blob/02e0378c33054acb0cdb8d731d1e2b2c2069bc1b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php#L119

$block = $resultPage->getLayout()->getBlock('catalog.wysiwyg.js');
1

If you want just the block and nothing else not sure if these answers are out of date but this worked for me.

    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var Http
     */
    protected $http;

    /**
     * Constructor
     *
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        PageFactory $resultPageFactory,
        Http $http
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->http = $http;
    }

    /**
     * Execute view action
     *
     * @return ResultInterface
     */
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $layout = $resultPage->getLayout();
        $block = $layout->createBlock('PixieMedia\AddPopup\Block\Addpopup\Paypal')
            ->setTemplate('PixieMedia_AddPopup::addpopup/paypal.phtml');
        return $this->http->setBody(
            $block->toHtml()
        );
    }
1
  • How can I specify layout via this method, i.e, in my case 'addpopup/paypal.phtml' has different child block specified for catalog_product_view layout and cart layout
    – Haris
    Commented Feb 16 at 7:04

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.