【问题标题】:Route-problem modular App with Zend and multilanguage support具有 Zend 和多语言支持的路由问题模块化应用程序
【发布时间】:2010-09-23 15:19:36
【问题描述】:

我在 Zend-Router 上苦苦挣扎。我想在我的网址中链接语言。一切正常,但我的模块化路由。

如果我调用:http://domain.de/en/index - 我的默认模块的 IndexController 的 indexAction 被执行和翻译。

同样适用: http://domain.de/en/about 于是调用了IndexController的aboutAction。

如果我调用:http://domain.de/en/forum/index 它应该执行论坛模块的 IndexController 的 indexAction。但事实并非如此。

我的目标是尽可能缩短我的网址,因此其中没有“默认”或“索引”。

你能帮我编辑我的 routes.xml 以便我得到想要的结果吗?

我的路由.xml

<config>
    <routes>
        <sc1 type="Zend_Controller_Router_Route">
            <route>:lang/:@module/:@controller/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc1>
        <sc2 type="Zend_Controller_Router_Route">
            <route>:lang/:@module/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc2>
        <sc3 type="Zend_Controller_Router_Route">
            <route>:lang/:@controller/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc3>
        <sc4 type="Zend_Controller_Router_Route">
            <route>:lang/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc4>
    </routes>
</config>

你有想法吗?

提前谢谢你, 托比亚斯

【问题讨论】:

    标签: php zend-framework zend-route


    【解决方案1】:

    我不太擅长 Zend Routes,但是查看您的代码,您需要一种方法来区分它应该转到哪个模块。您已将其设置为所有事件的默认值。如果它可以像您拥有的那样是动态的,那就太好了,但是我认为您需要专门为论坛等设置路由。鉴于 Zend 需要知道将其发送到哪里。如果您可以弄清楚如何使用 :@module 变量将其发送到该模块,那将起作用,但我不知道/认为这是可能的。

    在阅读完手册后,我为您想出了这个结构。您必须定义每个项目,例如论坛,您希望它重定向到如下所示。

    <config>
        <routes>
            <language type="Zend_Controller_Router_Route">
                <route>:language</route>
                <reqs language="[a-z]{2}">
            </language>
            <index type="Zend_Controller_Router_Route_Static">
                <route></route>
                <defaults module="default" controller="index" action="index" />
            </index>
            <about type="Zend_Controller_Router_Route_Static">
                <route>about</route>
                <defaults module="default" controller="index" action="about" />
            </about>
            <forums type="Zend_Controller_Router_Route_Static">
                <route>forums</route>
                <defaults module="forums" controller="index" action="index" />
            </forums>
            <lang-forums type="Zend_Controller_Router_Route_Chain">
                <chain>language, forums</chain>
            </lang-forums>
            <lang-about type="Zend_Controller_Router_Route_Chain">
                <chain>language, about</chain>
            </lang-about>
        </routes>
    </config>
    

    我不是 100% 确定 about 部分,特别是链接,但弄乱它应该会给你正确的方法。

    编辑

    作为另一个可能的例子,下面的内容是完整的。我想我上面的答案是正确的。

    看着Zend Routes Manual,如何设置它非常令人困惑。我也不喜欢以 xml 格式工作,我更喜欢 .ini,但这里有一个关于它应该如何工作的“伪模型”(未经测试,因为它很难测试):

    lang.index.type = "Zend_Controller_Router_Route"
    lang.index.route = ":language"
    lang.index.defaults.controller = index
    lang.index.defaults.action = index
    
    lang.forums.index.type =  "Zend_Controller_Router_Route_Static"
    lang.forums.index.route =  "forums"
    lang.forums.index.controller =  forums
    lang.forums.index.action =  index
    
    lang.forums.register.type =  "Zend_Controller_Router_Route_Static"
    lang.forums.register.route =  "forums/register"
    lang.forums.register.controller =  forums
    lang.forums.register.action =  register
    
    lang.forums.login.type = "Zend_Controller_Router_Route_Static"
    lang.forums.login.route = "forums/login"
    lang.forums.login.controller = forums
    lang.forums.login.action = login
    
    lang.forums.view.type = "Zend_Controller_Router_Route_Static"
    lang.forums.view.route = "forums/thread/:slug"
    lang.forums.view.controller = forums
    lang.forums.view.action = view
    
    lang.other.index.type = "Zend_Controller_Router_Route_Static"
    lang.other.index.route = "other"
    lang.other.index.controller = other
    lang.other.index.action = index
    
    lang.other.view.type = "Zend_Controller_Router_Route_Static"
    lang.other.view.route = "other/:slug"
    lang.other.view.controller = other
    lang.other.view.action = view
    

    希望这能让你正确地思考你需要做什么。如果其他人知道如何动态地做到这一点,我会完全感兴趣!我将研究 xml 方法,看看我是否无法从糟糕的文档中正确地解读它。

    【讨论】:

    • 但是没有比手动声明所有内容更好的方法吗?我的意思是模块化路线工作得很好。只有当我想链接语言时,我的模块才无法访问。
    • 我不知道。如果您可以测试/弄清楚,我会很想知道。我认为可能的唯一方法是在声明中使用变量,即:&lt;module&gt;@:module&lt;/module&gt;,但我非常怀疑这是可能的。
    猜你喜欢
    • 2018-01-26
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多