0

我在 Magento 2.4.x 中遇到页面布局问题

现在我已经在一个测试模块中创建了一个测试页面。

按照所有必要的步骤在页面上使用默认主题显示一些文本。我看到了输出,但页面布局损坏。

我想知道我在这个过程中做错了什么。

请指教。

模块文件如下

路线:Tadawi\Helloworld5\etc\frontend\routes.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworldroute" frontName="helloworld">
            <module name="Tadawi_Helloworld5"/>
        </route>
    </router>
</config>

控制器:Tadawi\Helloworld5\Controller\Index\Index.php

<?php
namespace Tadawi\Helloworld5\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action
{

    /** @var \Magento\Framework\View\Result\PageFactory  */
    protected $resultPageFactory;

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

布局文件:Tadawi\Helloworld5\view\frontend\layout\helloworldroute_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Tadawi\Helloworld5\Block\Index" name="helloworld.index" template="Tadawi_Helloworld5::index.phtml" />
    </referenceContainer>
</page>

模板文件:Tadawi\Helloworld5\view\frontend\templates\index.phtml

<h2>Hello World</h2>

<p>Hello and welcome!</p>

如果我调用路由,这是我得到的输出

在此处输入图像描述

4

1 回答 1

0

您还需要添加您在此提到的此文件块文件。

或者您可以简单地添加 '\Magento\Framework\View\Element\Template' 而不是类

也尝试在命令下面运行,然后检查

php bin/magento se:up php bin/magento s:d:c php bin/magento s:s:d -f php bin/magento c:f

可能这将帮助您解决问题。

于 2021-12-09T09:01:39.390 回答