Làm thế nào để bạn gọi Blocks trong Trình điều khiển Magento2?


8

Tôi muốn getBlock Adb/Block/Index in controller Adb/Controller/Category/View. Vậy làm thế nào để làm điều đó?

Biên tập

trong bộ điều khiển.

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');

    }
}

bố trí ở lối vào

<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> 

Nó không gọi khối Faq


1
Bạn không nên gọi các khối trong bộ điều khiển. Tại sao bạn cần nó?
— KAndy

Câu trả lời:


4

Trả về của bạn không có gì trong lệnh thực thi của bạn. Trả về trang kết quả trong bộ điều khiển của bạn

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

    return $resultPage;

tên xml của bạn phải là faq_carget_view.xml.


20

Nếu bạn muốn khởi tạo một khối, bạn cần thêm vào để tạo trong hàm tạo một thể hiện của \Magento\Framework\View\LayoutFactory

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

Sau đó, bạn có thể khởi tạo một khối như thế này:

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

nếu bạn muốn truy cập vào một khối được xác định trong bố cục, bạn cần thêm vào hàm tạo và thể hiện của \Magento\Framework\View\Result\PageFactory

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

Sau đó, bạn sẽ có thể truy cập vào khối như thế này:

$resultPage = $this->resultPageFactory->create();
$blockInstance = $resultPage->getLayout()->getBlock('block.name.here');

Đây vẫn là cách gọi khối được đề nghị ? Tôi không thể tiêm? :)
— treyBake

9
  1. Tạo khối:

     <?php
        namespace Training\Test\Block;
        class Test extends \Magento\Framework\View\Element\AbstractBlock
        {
           protected function _toHtml() {
             return "<b>Hello world from block!</b>";
           }
        }
  2. Tạo một lớp hành động:

    <?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
Không sử dụng $this->_viewtrong bộ điều khiển. Tôi đọc nó là @deprecated. Sử dụng resultPage->getLayout()thay vì như câu trả lời khác cho thấy.
— 7ochem

5

Xem mã cốt lõi:

Bước 1

https://github.com/magento/magento2/blob/02e0378c33054acb0cdb8d731d1e2b2c2069bc1b/app/code/Magento/Catalog/Contoder/Adminhtml/Carget/Edit.php

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

Bước 2

https://github.com/magento/magento2/blob/02e0378c33054acb0cdb8d731d1e2b2c2069bc1b/app/code/Magento/Catalog/Contoder/Adminhtml/C Category / Edit /

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

Bước 3

https://github.com/magento/magento2/blob/02e0378c33054acb0cdb8d731d1e2b2c2069bc1b/app/code/Magento/Catalog/Contoder/Adminhtml/Carget/Edit.php

$block = $resultPage->getLayout()->getBlock('catalog.wysiwyg.js');
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.