You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2中添加manufacturer新页面方法及模块主题关联疑问

Magento 2 添加/manufacturer页面及app/code与app/design关联方案

1. 先完成app/code里的模块核心配置(路由+控制器)

假设你已创建自定义模块(示例:Vendor/Manufacturer),需确保以下文件配置正确:

1.1 注册模块(app/code/Vendor/Manufacturer/registration.php)

<?php
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Vendor_Manufacturer',
    __DIR__
);

1.2 模块配置文件(app/code/Vendor/Manufacturer/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_Manufacturer" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Framework"/>
            <module name="Magento_Store"/>
        </sequence>
    </module>
</config>

1.3 配置前端路由(app/code/Vendor/Manufacturer/etc/frontend/routes.xml)

指定manufacturer路径对应的路由规则:

<?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="manufacturer" frontName="manufacturer">
            <module name="Vendor_Manufacturer"/>
        </route>
    </router>
</config>

1.4 创建控制器(app/code/Vendor/Manufacturer/Controller/Index/Index.php)

处理请求并加载页面布局:

<?php
namespace Vendor\Manufacturer\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;

class Index implements HttpGetActionInterface
{
    protected $resultPageFactory;

    public function __construct(PageFactory $resultPageFactory)
    {
        $this->resultPageFactory = $resultPageFactory;
    }

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

2. app/code与app/design的关联逻辑

app/code的模块通过布局文件指定模板引用规则,app/design的主题负责提供模板的具体实现(或覆盖默认模板),核心关联点如下:

2.1 在模块中定义布局(app/code/Vendor/Manufacturer/view/frontend/layout/manufacturer_index_index.xml)

该文件定义页面结构并关联模板:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Manufacturer Page</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="manufacturer.page" template="Vendor_Manufacturer::manufacturer.phtml"/>
        </referenceContainer>
    </body>
</page>

其中Vendor_Manufacturer::manufacturer.phtml是关键关联标识:Vendor_Manufacturer为模块名,manufacturer.phtml为模板文件名,Magento会优先在模块的view/frontend/templates目录查找,再按主题 fallback 规则去app/design目录查找。

2.2 在主题中自定义模板(可选)

若需定制页面样式,在主题目录(示例:app/design/frontend/Vendor/your_theme/Vendor_Manufacturer/templates/manufacturer.phtml)创建模板文件:

<h1>品牌商列表</h1>
<p>这里是品牌商页面的自定义内容</p>
<!-- 可添加Magento动态组件、数据渲染逻辑等 -->

主题中的同名模板会覆盖模块默认模板,这是Magento的模板 fallback 机制。

3. 执行必要的命令生效配置

bin/magento module:enable Vendor_Manufacturer
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento cache:flush

完成以上步骤后,访问https://xxxx/manufacturer即可看到自定义页面。

内容的提问来源于stack exchange,提问作者Bohdan V.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:10:24