简体   繁体   English

从Observer Magento 2加载.phtml模板

[英]Load .phtml template from Observer Magento 2

I'm trying to load my .phtml files in the template folder within my observer but I'm getting this error 我正在尝试将我的.phtml文件加载到观察器内的模板文件夹中,但出现此错误

Invalid template file: 'VENDOR_MYModule::Category/index.phtml' in module: 'VENDOR_MYModule' block's name: 'category_0' 模块中的无效模板文件:“ VENDOR_MYModule :: Category / index.phtml”:“ VENDOR_MYModule”块的名称:“ category_0”

Here's the structure of my files 这是我文件的结构

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

Now the content of my Block/Category.php is below 现在,我的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);
    }

}

The content of my Controller/Category/Index.php is below 我的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();
    }   
}

The content of the layout/header_category_index.xml is below 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>

The content of my .phtml is just a simple <h1>Hello world</h1> . 我的.phtml的内容只是一个简单的<h1>Hello world</h1> Now in my Observer I'm trying to load this .phtml file but I can't load it and getting the error. 现在,在我的Observer我试图加载此.phtml文件,但无法加载它并得到错误。 The content of my Observer Observer\\CategoryObserver is below 我的Observer 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]);
}

Here's the content of my events.xml 这是我的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>

But I'm getting the error as mentioned above. 但是我遇到了如上所述的错误。 Any idea on how to load this .phtml file to the observer? 关于如何将该.phtml文件加载到观察者的任何想法吗? I'm planning to write the content of this .phtml file to a .txt file. 我打算将此.phtml文件的内容写入.txt文件。 But I can't proceed since I tried outputting it but I'm still getting an error 但是由于尝试输出它所以无法继续,但仍然出现错误

UPDATE: 更新:

Tried my code using the Frontend controller/action access and the block successfully loaded. 使用前端控制器/操作访问权限尝试了我的代码,并成功加载了该块。 Now I think there's another way or implementation when retrieving the .phtml in the Admin part or in the Observer. 现在,我认为在Admin部分或Observer中检索.phtml时,还有另一种方法或实现。 Also note that the observer is triggered when I try to edit/save a catalog->category. 还要注意,当我尝试编辑/保存目录->类别时,将触发观察者。

could you please check that you are calling following class as $layout in your observer: 您能否检查一下您是否在观察员中将以下类称为$ layout:

\\Magento\\Framework\\View\\LayoutFactory $layoutFactory \\ Magento \\ Framework \\ View \\ LayoutFactory $ layoutFactory

In addition, you need to set response header and body instead of returning html. 此外,您需要设置响应标头和正文,而不是返回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;
}

Hope this may help you! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM