从Observer Magento 2加载.phtml模板

问题描述 投票:0回答:1

我正在尝试将我的.phtml文件加载到我的观察者的模板文件夹中,但是我收到了这个错误

模板文件无效:模块中的'VENDOR_MYModule :: Category / index.phtml':'VENDOR_MYModule'块名称:'category_0'

这是我的文件的结构

app
 + code
    + VENDOR
      + MYModule
        + Block
            - Category.php
        + Controller
            + Category
               - Index.php
        + etc
            + frontend
                - routes.xml
        + Observer
            - CategoryObserver.php
        + view
            + frontend
                + layout
                    - header_category_index.xml
                + templates
                    + category
                        - index.phtml

现在我的Block/Category.php的内容在下面

<?php
namespace VENDOR\MYModule\Block;

class Category extends \Magento\Framework\View\Element\Template 
{
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,       
        array $data = []
    ){
        parent::__construct($context, $data);
    }

}

我的Controller/Category/Index.php的内容如下

<?php
namespace VENDOR\MYModule\Controller\Category;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;    

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

    public function execute()
    {       
        return $this->_pageFactory->create();
    }   
}

layout/header_category_index.xml的内容如下

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="VENDOR\MYModule\Block\Category" name="category_items" template="VENDOR_MYModule::category/index.phtml" />
    </referenceContainer>
</page>

我的.phtml的内容只是一个简单的<h1>Hello world</h1>。现在在我的Observer我正在尝试加载这个.phtml文件,但我无法加载它并得到错误。观察员Observer\CategoryObserver的内容如下

public function execute(\Magento\Framework\Event\Observer $observer)
{           
    $layout = $this->_layout->create();
    $block = $layout->createBlock('VENDOR\MYModule\Block\Category')->setTemplate('VENDOR_MYModule::Category/index.phtml')->toHtml();        

    $this->_logger->debug("[DEBUG]::" , [$block]);
}

这是我的events.xml的内容

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_category_save_after">
        <observer name="category-edit" instance="VENDOR\MYModule\Observer\CategoryObserver" />
    </event>   
</config>

但是我收到了上面提到的错误。有关如何将此.phtml文件加载到观察者的任何想法?我打算将此.phtml文件的内容写入.txt文件。但我无法继续,因为我尝试输出它但我仍然收到错误

更新:

使用前端控制器/操作访问尝试我的代码并成功加载块。现在我认为在Admin部分或Observer中检索.phtml时还有另一种方法或实现。另请注意,当我尝试编辑/保存目录 - >类别时会触发观察者。

magento magento2 magento-2.0 magento2.2 magento2.1
1个回答
0
投票

请问你在观察员中检查你是否在课堂上调用$ layout:

\ Magento \ Framework \ View \ LayoutFactory $ layoutFactory

此外,您需要设置响应标头和正文而不是返回html。

protected $_layoutFactory;

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

public function execute(\Magento\Framework\Event\Observer $observer)
{           
    $layout = $this->_layoutFactory->create();
    $block = $layout->createBlock('VENDOR\MYModule\Block\Category')->setTemplate('VENDOR_MYModule::Category/index.phtml')->toHtml();

    $response = $observer->getEvent()->getData('response');
    $response->setHeader('Content-Type','text/html')->setBody($block->toHtml());
    return;
}

希望这可以帮到你!

© www.soinside.com 2019 - 2024. All rights reserved.