【问题标题】:Make Magento render complete HTML page让 Magento 渲染完整的 HTML 页面
【发布时间】:2017-08-23 10:24:03
【问题描述】:

我是 Magento 的新手

我得到了不想拆分成多个块的模板。当我转到某个 url 时,我只想按原样呈现页面(不仅如此,但这是我以后可以尝试理解的内容)。

例如,转到/route/controller/action 应该会显示一个我从<HTML></HTML> 编写的html 页面(当然包括HEAD 和BODY)。

所以我读了一些东西。我遵循了一些官方教程,可以创建一个模块和一个动作来呈现 JSON。工作正常!转到 /test/page/view 会显示一个愚蠢的 json。现在我想做一些类似的事情来渲染一个页面(并最终将参数传递给它)

(http://devdocs.magento.com/videos/fundamentals/create-a-new-module/)

这就是我现在得到的:

/Vendor/Module/etc/frontend/routes.xml

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

/Vendor/Module/Controller/Page/View.php

namespace Victor\Template\Controller\Page;

use \Magento\Backend\App\Action\Context;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\Controller\Result\JsonFactory;
use \Magento\Framework\View\Result\PageFactory;

class View extends Action
{
    protected $resultPageFactory;

    function __construct(Context $context, PageFactory $resultPageFactory){
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

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

.../view/frontend/layout/template_page_view.xml(可能错误就在这里)

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Victor\Template\Block\Page\View" name="page.view" template="Victor_Template::page/view.phtml"/>
        </referenceContainer>
    </body>
</page>

.../view/frontend/templates/page/view.phtml

<h1>Hello World!</h1>

在清理缓存、刷新缓存、授予权限和升级设置(我必须在代码中发生一些更改时一直做的事情)之后,我得到一个 HTTP 代码 200,它给我一个空白(全白)Magento 页面其中包含对 css、javascript 文件的引用和类似这样的怪异:

&lt;body data-container="body" data-mage-init='{"loaderAjax": {}, "loader": { "icon": "http://localhost/magento/pub/static/version1490812084/frontend/Magento/luma/en_US/images/loader-2.gif"}}' class="victor-page-view page-layout-admin-1column"&gt;

无论如何,我不知道后台发生了什么。有些东西告诉我错误是布局的定义 (template_page_view)

我尝试在视图中的execute() 方法中的return 之前使用echo,但它没有出现,exit 也不会阻止要呈现的 magento 黑白页面.

我希望回复只是&lt;h1&gt;Hwllo World!&lt;/h1&gt;

我做错了什么以及在 Magento 上做错的正确方法是什么?

@编辑 好吧,似乎有很多缓存问题。现在echoexit 工作。但我仍然没有得到 Hello world

【问题讨论】:

标签: php magento view routes magento2


【解决方案1】:

这并不容易,但试试这个:

1) 在您的布局 xml 文件 (view/frontend/layout/template_page_view.xml) 中:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="empty" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="top.container" remove="true" />
        <referenceContainer name="top.container" remove="true" />
        <referenceContainer name="page.bottom.container" remove="true"/>
        <referenceContainer name="before.body.end" remove="true"/>
        <referenceContainer name="after.body.start" remove="true"/>
        <referenceContainer name="footer" remove="true" />
        <referenceContainer name="footer-container" remove="true" />
        <referenceContainer name="global.notices" remove="true" />

        <referenceContainer name="content">
            <block class="Victor\Template\Block\Page\View" name="page.view" template="Victor_Template::page/view.phtml"/>
        </referenceContainer>
    </body>
</page>

您可能需要添加更多容器以删除,具体取决于您的主题和任何其他 3rd 方模块

2) 在您的控制器中创建 Page 对象,如下所示:

return $this->resultPageFactory->create(false, ['template' => 'Vendrom_Module::blank.phtml']);

3) 最后创建文件../view/frontend/blank.phtml

<!doctype html>
<html>
<body>
<?php /* @escapeNotVerified */ echo $layoutContent ?>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2015-06-04
    • 1970-01-01
    • 2016-05-24
    • 2011-06-28
    • 2019-04-01
    • 2021-10-25
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多