Php 在Magento 2中,如何覆盖phtml或布局核心文件?
我在Magento 2中开发了“Hello world”扩展 我想覆盖核心文件的“联系我们”表单。在Magento 2中覆盖Contact us表单文件的正确方法是什么Php 在Magento 2中,如何覆盖phtml或布局核心文件?,php,magento,magento2,Php,Magento,Magento2,我在Magento 2中开发了“Hello world”扩展 我想覆盖核心文件的“联系我们”表单。在Magento 2中覆盖Contact us表单文件的正确方法是什么 请帮帮我。任何帮助都将不胜感激。为此,您需要创建一个扩展(自定义模块) 从app/magento创建块、等和视图文件夹 在etc文件夹中创建module.xml: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-
请帮帮我。任何帮助都将不胜感激。为此,您需要创建一个扩展(自定义模块) 从app/magento创建
块
、等
和视图
文件夹
在etc
文件夹中创建module.xml
:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Xyz_Contact" setup_version="0.0.1"></module>
</config>
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<title>Contact Us</title>
</head>
<body>
<referenceContainer name="content">
<block class="Xyz\Contact\Block\ContactForm" name="contactForm" template="Xyz_Contact::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
</body>
</page>
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<form class="form contact"
action="<?php echo $block->getFormAction(); ?>"
id="contact-form"
method="post"
data-hasrequired="<?php echo __('* Required Fields') ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?php echo __('Write Us') ?></span></legend><br />
<div class="field note no-label"><?php echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div>
<div class="field name required">
<label class="label" for="name"><span><?php echo __('Name') ?></span></label>
<div class="control">
<input name="name" id="name" title="<?php echo __('Name') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field email required">
<label class="label" for="email"><span><?php echo __('Email') ?></span></label>
<div class="control">
<input name="email" id="email" title="<?php echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field telephone">
<label class="label" for="telephone"><span><?php echo __('Phone Number') ?></span></label>
<div class="control">
<input name="telephone" id="telephone" title="<?php echo __('Phone Number') ?>" value="" class="input-text" type="text" />
</div>
</div>
<div class="field comment required">
<label class="label" for="comment"><span><?php echo __('What’s on your mind?') ?></span></label>
<div class="control">
<textarea name="comment" id="comment" title="<?php echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"></textarea>
</div>
</div>
<?php echo $block->getChildHtml('form.additional.info'); ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?php echo __('Submit') ?>" class="action submit primary">
<span><?php echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
创建模板
文件夹并将下面的代码放入表单。phtml
:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Xyz_Contact" setup_version="0.0.1"></module>
</config>
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<title>Contact Us</title>
</head>
<body>
<referenceContainer name="content">
<block class="Xyz\Contact\Block\ContactForm" name="contactForm" template="Xyz_Contact::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
</body>
</page>
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<form class="form contact"
action="<?php echo $block->getFormAction(); ?>"
id="contact-form"
method="post"
data-hasrequired="<?php echo __('* Required Fields') ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?php echo __('Write Us') ?></span></legend><br />
<div class="field note no-label"><?php echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div>
<div class="field name required">
<label class="label" for="name"><span><?php echo __('Name') ?></span></label>
<div class="control">
<input name="name" id="name" title="<?php echo __('Name') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field email required">
<label class="label" for="email"><span><?php echo __('Email') ?></span></label>
<div class="control">
<input name="email" id="email" title="<?php echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field telephone">
<label class="label" for="telephone"><span><?php echo __('Phone Number') ?></span></label>
<div class="control">
<input name="telephone" id="telephone" title="<?php echo __('Phone Number') ?>" value="" class="input-text" type="text" />
</div>
</div>
<div class="field comment required">
<label class="label" for="comment"><span><?php echo __('What’s on your mind?') ?></span></label>
<div class="control">
<textarea name="comment" id="comment" title="<?php echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"></textarea>
</div>
</div>
<?php echo $block->getChildHtml('form.additional.info'); ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?php echo __('Submit') ?>" class="action submit primary">
<span><?php echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
请不要忘记在app/etc/config.php
中注册您的模块,或者使用命令行中的Magento二进制工具:php-f bin/Magento模块:启用Xyz_联系人
这里,Xyz
是公司(供应商)的名称,Contact
是模块名称
如果您有任何问题,请告诉我。您可以使用插件来完成 首先,您必须重写该块并调用
beforeToHtml
方法,如下所示:
public function beforeToHtml(\Magento\Catalog\Block\Product\View\Description $originalBlock)
{
$originalBlock->setTemplate('Vendorname_Modulename::description.phtml');
}
与前面的两个答案不同,我选择从布局中删除原始块,并使用自己的模板添加一个新块 我们将创建一个新模块,
VendorName\u ModuleName
,我们需要为其创建以下文件:
/app/code/VendorName/ModuleName/view/frontend/layout/contact_index_index.xml
/app/code/VendorName/ModuleName/view/frontend/templates/form.phtml
/app/code/VendorName/ModuleName/etc/module.xml
/app/code/VendorName/ModuleName/composer.json
/app/code/VendorName/ModuleName/registration.php
联系_index_index.xml
registration.php 只需将
VendorName\u ModuleName
替换为您自己的
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
composer.json 当然,如果您想让新模块正常工作,请不要忘记添加
composer.json
{
"name": "VendorName/ModuleName",
"autoload": {
"psr-4": { "VendorName\\ModuleName\\": "" },
"files": [ "registration.php" ]
} }
进一步参考
我解决了这个问题。如果要覆盖任何核心文件,只需使用引用名称和传递给referenceBlock name=“passit”的引用名称 对于conatct us file override,首先获取contactus form.phtml的原始文件,然后找到布局文件contact_index_index.xml,并获取参考名称,如“contactForm” System/core contact\u index\u index.xml文件。
<referenceContainer name="content">
<block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
此“contactForm”引用名称在referenceBlock标记中传递给我们的扩展布局文件。请显示以下代码
我们的扩展布局contact\u index\u index.xml文件
<referenceBlock name="contactForm">
<action method="setTemplate">
<argument name="template"xsi:type="string">Test_Overide::form.phtml</argument>
</action>
</referenceBlock>
测试覆盖::form.phtml
在此之后,系统contactus form.phtml未被调用,扩展名form.phtml文件被调用。您可以使用开发人员前端调试工具进行检查。Hi覆盖核心模板文件的最简单方法:-
module-contact/view/frontend/templates/form.phtml
转到您的主题app/design/frontend/vendor/your_theme/
请遵循以下步骤:
form.phtm
或从core复制form.phtml
,然后对其进行编辑你们已经得到了什么?当我使用contact us phtml引用名调用core contact us form.phtml时,我得到了空白页。在magento 1.x中,使用引用名,我们可以覆盖core phtml文件,对吗?但在magento 2中结构不同。我不明白如何覆盖magento 2中的核心文件?谢谢您的帮助。但这段代码不适用于我。contact_index_index.xml文件中的模块未被调用。。默认情况下,当我在浏览器中发送请求时,名为magento的原始contact_index_index.xml文件会进入我的控制器。在我的控制器$this->_view->renderLayout()中;函数调用。。因此,它直接调用了Megento contact us layout file,而不是我的layout file.Hi@Niks-只是想说明一下,这个答案可能是基于Magento 2的前GA版本,可能需要更新。现在,布局和模板应该位于
视图/前端/布局
或模板
-此答案缺少该路径的前端
部分。当然,解决方案更可取,而不是添加插件来重新创建相同的功能?此答案的优缺点有什么区别,然后将form.phtml复制到“app/design/frontend/vendor/your_theme/Magento_Contact”?
module-contact/view/frontend/templates/form.phtml