【问题标题】:Implementing Zend_Locale and Zend_Translate with modules用模块实现 Zend_Locale 和 Zend_Translate
【发布时间】:2011-12-03 13:50:41
【问题描述】:

我在互联网上搜索了将 Zend_Locale 和 Zend_Translate 集成到模块化结构中的良好解决方案。这是我想要的结束路径:

http://url/:lang/:module/:controller/:action
http://url/:lang/:controller/:action
http://url/:module/:controller/:action

我看过很多使用路由器的教程,但似乎没有一个适合我。谁能帮我解决这个问题。

谢谢!

【问题讨论】:

    标签: zend-framework zend-translate zend-locale


    【解决方案1】:

    这里的第一个问题是,在使用路由时,您需要有非常具体的约束才能将规则与这么多变量匹配。

    我建议不要同时接受 //url/:lang/:module/:controller/:action 和 //url/:module/:controller/:action 只使用第一个结构。这样就更容易将第一个规则与 //url/:lang/:controller/:action 隔离开来,每个规则只有 2 个规则,每个规则都有不同的“单词”(URL 部分)。

    $withModule = new Zend_Controller_Router_Route(
        ':lang/:module/:controller/:action',
        array()
    );
    
    $withoutModule = new Zend_Controller_Router_Route(
        ':lang/:controller/:action',
        array(
            'module' => 'default'
        )
    );
    
    $router->addRoute('withModule', $withModule);
    $router->addRoute('withoutModule', $withoutModule);
    

    在第一个路由中,您没有指定任何默认值,因此它与用于第二个路由的 URL 不匹配,而在第二个路由中,您默认了模块(因为 ZF 需要该信息)。

    至于第三条规则,我建议使用基本祖先控制器,例如 MyLib_Controller 并在其 init() 方法中验证是否接收到语言参数,如下例所示:

    if(!$this->_getParam('lang')) {
        //this should cover the 3rd use case
        $this->_redirect('/en/' + $this->view->url(), array('exit' => true, 'prependBase' => false));
    } else {
        //setup Zend_Translate
    }
    

    另一种可能性是将 :lang 变量限制为仅包含 2 个字母的单词,但这会产生问题,我更愿意避免它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      相关资源
      最近更新 更多