1

我正在尝试修改两个类似的自定义模块以将它们组合成 1 个自定义模块。但是,这两个模块都使用<routers>confix.xml 中的标记。我已经搜索并搜索了一种方法来做到这一点,但没有运气。

例如:

自定义模块 1

<admin>
    <routers>
        <Namespace_Module1>
             <use>admin</use>
            <args>
                <module>Namespace_Module1</module>
                <frontName>frontname</frontName>
            </args>
        </Namespace_Module1>
    </routers>
</admin>

自定义模块 2

<admin>
    <routers>
        <Namespace_Module1>
             <use>admin</use>
            <args>
                <module>Namespace_Module2</module>
                <frontName>namefront</frontName>
            </args>
        </Namespace_Module2>
    </routers>
</admin>

我认为这是阻止我将两个相似模块组合在一起的唯一原因。这可以做到吗?如果是这样,如何(一个例子)?

4

2 回答 2

4

Im not entirely sure what you are trying to achieve. Are you looking for two different routers i.e. two distinct front names, defined from within a single module?. If so, read on - if not, can you clarify things a little further please.

Firstly, you have a tag mismatch in the xml you have provided in Custom Module 2. You are opening <Namespace_Module1> and closing with </Namespace_Module2> - so as it stands, this code will not work.

Secondly, to define a router, you must use the <routers> tag - so there is no issue with this. The nodes directly underneath it must be unique though.

So, assuming I have read your question correctly and you want to merge these two router nodes but still have two distinct front names, the following would work:

<admin>
    <routers>
        <namespace_module1>
            <use>admin</use>
            <args>
                <module>Namespace_Module1</module>
                <frontName>frontname</frontName>
            </args>
        </namespace_module1>
        <namespace_module2>
             <use>admin</use>
            <args>
                <module>Namespace_Module2</module>
                <frontName>namefront</frontName>
            </args>
        </namespace_module2>
    </routers>
</admin>

Though, if there was a particular reason that these have to be separate routers, then I would offer the following as better alternative: use a single router but multiple controllers. So your xml would just be:

<admin>
    <routers>
        <namespace_module>
            <use>admin</use>
            <args>
                <module>Namespace_Module</module>
                <frontName>frontname</frontName>
            </args>
        </namespace_module>
    </routers>
</admin>

Create two controllers in your modules controller directory, say Module1Controller.php and Module2Controller.php.

Then you would be able to access them (the index actions) via /frontname/module1/ and /frontname/module2.

This feels like a much cleaner solution.

于 2012-06-11T21:34:40.883 回答
0

据我了解,您正在尝试通过一个 frontName 匹配 2 个模块的控制器。

这完全可以通过在第二个模块中使用“模块”节点来实现。保留第一个模块的配置,但将其用于第二个模块:

<admin>
    <routers>
        <Namespace_Module1>
            <args>
                <modules>
                    <Namespace_Module2>Namespace_Module2</Namespace_Module2>
                </modules>
            </args>
        </Namespace_Module1>
    </routers>
</admin>

无需指定 'use' 或 'frontName',这些 XML 文件无论如何都会合并。Magento 将首先尝试在第一个模块中找到匹配的控制器,然后在第二个模块中。

于 2012-06-12T07:24:55.023 回答