Magento 2.1.10:如何在自定义模块中重写pub/errors/default/503.phtml
如何在Magento 2.1.10中重写pub/errors/default/503.phtml
嘿,针对Magento 2.1.10版本的503错误页面重写,我给你分两种方案——一种是快速应急的直接修改,另一种是符合Magento开发规范的自定义模块重写(强烈推荐后者,避免升级时丢失修改)。
方案1:直接修改(不推荐,仅应急使用)
如果你只是临时改一下应付紧急情况,可以直接找到文件 pub/errors/default/503.phtml,打开编辑修改内容就行。但要注意:这种方式修改的是核心文件,后续Magento版本升级时会被覆盖,所以不建议长期使用。
方案2:通过自定义模块重写(推荐)
用自定义模块来重写是最规范的方式,步骤如下:
1. 创建自定义模块的基础结构
假设你的模块命名为 Vendor_CustomError(把Vendor换成你的厂商名,CustomError换成模块名),需要创建以下文件:
app/code/Vendor/CustomError/registration.php<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_CustomError', __DIR__ );app/code/Vendor/CustomError/etc/module.xml<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_CustomError" setup_version="1.0.0"> <sequence> <module name="Magento_Error"/> </sequence> </module> </config>
2. 创建错误页面重写配置
在模块目录下创建 errors/etc/xml/error.xml 文件,用来指定替换503模板的路径:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Error/etc/error.xsd"> <default> <error type="503"> <template>Vendor_CustomError::errors/503.phtml</template> </error> </default> </config>
3. 复制并修改503.phtml文件
把原文件 pub/errors/default/503.phtml 的内容复制到你的模块路径 app/code/Vendor/CustomError/view/frontend/templates/errors/503.phtml,然后根据需求修改这个文件里的内容,比如调整文案、样式、添加品牌元素等。
4. 启用模块并清理缓存
执行以下命令启用模块并刷新缓存:
php bin/magento module:enable Vendor_CustomError php bin/magento setup:upgrade php bin/magento cache:clean
这样就完成了通过自定义模块重写503错误页面的操作,后续升级Magento时你的修改不会被覆盖,完全符合开发规范。
内容的提问来源于stack exchange,提问作者Hardik Makwana
相关产品推荐
相关产品推荐

