[英]Load .phtml template from Observer Magento 2
我正在嘗試將我的.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 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時,還有另一種方法或實現。 還要注意,當我嘗試編輯/保存目錄->類別時,將觸發觀察者。
您能否檢查一下您是否在觀察員中將以下類稱為$ 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;
}
希望對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.