【发布时间】: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 文件的引用和类似这样的怪异:
<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">
无论如何,我不知道后台发生了什么。有些东西告诉我错误是布局的定义 (template_page_view)
我尝试在视图中的execute() 方法中的return 之前使用echo,但它没有出现,exit 也不会阻止要呈现的 magento 黑白页面.
我希望回复只是<h1>Hwllo World!</h1>。
我做错了什么以及在 Magento 上做错的正确方法是什么?
@编辑
好吧,似乎有很多缓存问题。现在echo 和exit 工作。但我仍然没有得到 Hello world
【问题讨论】:
标签: php magento view routes magento2