使用模块实现Zend_Locale和Zend_Translate

时间:2011-12-03 13:50:41

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

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

http://url/:lang/:module/:controller/:action
http://url/:lang/:controller/:action
http://url/:module/:controller/:action< =应采用默认语言环境

我见过许多使用路由器的教程,但似乎没有一个对我有用。有人可以为我解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

这里的第一个问题是,当使用路由时,您需要具有非常特定的约束,以便将规则与那么多变量匹配。

我建议不要同时接受// url /:lang /:module /:controller /:action和// url /:module /:controller /:action sett只使用第一个结构。这样就可以更容易地将第一个规则与// 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个字母单词,但这会产生问题,我宁愿避免它。

相关问题