how do you call Blocks in Controller Magento2?
-
13-12-2019 - |
Question
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
Solution
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.
OTHER TIPS
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');
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>"; } }
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()); } }
See the core code:
Step 1
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
Step 2
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
Step 3
$block = $resultPage->getLayout()->getBlock('catalog.wysiwyg.js');