come si chiama Blocchi Controller Magento2?
-
13-12-2019 - |
Domanda
Voglio getBlock Adb/Block/Index in controller Adb/Controller/Category/View
.Quindi, Come fare?
Modifica
in un 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>
Non blocco chiamata Faq
Soluzione
Il tuo ritorno nulla nel tuo comando EXECUTE.Ritorno Risultato nel tuo controller
$resultPage = $this->resultPageFactory->create();
return $resultPage;
.
Il tuo nome XML dovrebbe essere faq_category_view.xml.
Altri suggerimenti
Se si desidera istanziare un blocco è necessario aggiungere per iniettare nel costruttore un'istanza di \Magento\Framework\View\LayoutFactory
...
protected $layoutFactory;
...
public function __construct(
...
\Magento\Framework\View\LayoutFactory $layoutFactory,
...
) {
...
$this->layoutFactory = $layoutFactory;
...
}
.
Quindi puoi istanziare un blocco come questo:
$this->layoutFactory->create()->createBlock('Block\Class\Here');
.
Se si desidera accedere a un blocco definito nel layout è necessario iniettare nel costruttore ed istanza di \Magento\Framework\View\Result\PageFactory
...
protected $resultPageFactory;
...
public function __construct(
...
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
...
) {
...
$this->resultPageFactory = $resultPageFactory;
...
}
.
Allora sarai in grado di accedere al blocco come questo:
$resultPage = $this->resultPageFactory->create();
$blockInstance = $resultPage->getLayout()->getBlock('block.name.here');
. Creare il blocco:
<?php namespace Training\Test\Block; class Test extends \Magento\Framework\View\Element\AbstractBlock { protected function _toHtml() { return "<b>Hello world from block!</b>"; } }
Creare un'azione di classe:
<?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()); } }
Vedi il codice principale:
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');
.