在 Zend 中使用多语言路由器
Posted
技术标签:
【中文标题】在 Zend 中使用多语言路由器【英文标题】:Working with multilanguage routers in Zend 【发布时间】:2011-09-12 21:33:42 【问题描述】:我正在开发一个包含两个模块(管理员和公共)的多语言 Zend 应用程序,我想在 url 中传递语言代码,所以在我的引导程序中我有:
受保护的函数_initAutoload()
$this->bootstrap('frontController');
$this->_front = $this->getResource('frontController');
$autoLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'admin/forms/',
'namespace' => 'Admin_Form_',
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
)
)
));
$autoLoader_ = new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH . '/public/',
'namespace' => 'Public_',
'resourceTypes' => array(
'forms' => array(
'path' => 'forms/',
'namespace' => 'Public_Form_'
)
)
));
return $autoLoader;
protected function _initConfig()
$config = new Zend_Config_Xml(BASE_PATH . '/config.xml', APPLICATION_ENV);
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);
return $config;
protected function _initDb()
$this->bootstrap('config');
$config = $this->getResource('config');
$db = Zend_Db::factory($config->database->adapter, $config->database);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$db->query("SET NAMES 'utf8';");
$db->query("SET CHARACTER SET 'utf8';");
Zend_Db_Table::setDefaultAdapter($db);
return $db;
protected function _initRoutes()
$router = $this->_front->getRouter();
$router->removeDefaultRoutes();
$language = new Zend_Controller_Router_Route(':language', array('language' => 'es'));
$module = new Zend_Controller_Router_Route_Module(
array(
'module' => 'public',
'controller' => 'index',
'action' => 'index'
),
$this->_front->getDispatcher(),
$this->_front->getRequest()
);
$module->isAbstract(true);
$default = new Zend_Controller_Router_Route_Chain();
$default->chain($language);
$default->chain($module);
$router->addRoute('default', $default);
http://domain.com/es/admin/ => 有效
http://domain.com/admin/ => 不起作用
http://domain.com/es/ => 有效
http://domain.com/ => 不起作用
问题是,即使我没有指定语言,我也希望它能够工作。我该怎么做?如何提取语言代码 (en) 以在_initLocale
中使用?
提前致谢
【问题讨论】:
【参考方案1】:这是我用于路由的引导初始化。它包含用于使用多语言路由以及使模块和 url halper 工作的所有解决方案:
public function _initRoutes()
$this->bootstrap('FrontController');
$this->_frontController = $this->getResource('FrontController');
$router = $this->_frontController->getRouter();
if (isset($_GET['lang']))
$lang = $_GET['lang'];
else
// auto recognition of language
$locale = new Zend_Locale();
$lang = $locale->getLanguage();
$langRoute = new Zend_Controller_Router_Route(
':lang/',
array(
'lang' => $lang
)
);
$defaultRoute = new Zend_Controller_Router_Route(
':controller/:action/*',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
)
);
$defaultRoute = $langRoute->chain($defaultRoute);
$adminRoute = new Zend_Controller_Router_Route(
'admin/:controller/:action/*',
array(
'module' => 'admin',
'controller' => 'index',
'action' => 'index'
)
);
$router->addRoute('langRoute', $langRoute);
$router->addRoute('defaultRoute', $defaultRoute);
$router->addRoute('adminRoute', $adminRoute);
【讨论】:
【参考方案2】:您需要链接语言路径中的所有路径。见这里:
http://robertbasic.com/blog/chaining-routes-in-zend-framework/
两个模块:
$defaultRoute = new Zend_Controller_Router_Route(
':module/:controller/:action',
【讨论】:
感谢回复,但是如果应用有两个模块呢?【参考方案3】:这是一个通用的插件,允许我们创建您自己的路线而无需记住语言。它还设置 Zend_Translator。这是一个基类,为了提高速度,我建议使用 Zend_Cache,因为下面的代码会影响代码效率(如果你有 +100 条路由,我会说它是必需的)。
<?php
class PsScripts_Controller_Plugin_Lang extends Zend_Controller_Plugin_Abstract
private function initTranslator($locale)
$translate = new Zend_Translate(array('adapter' => 'tmx',
'content' => APPLICATION_PATH . '/configs/translations.tmx',
'locale' => $locale));
Zend_Registry::set('Zend_Translate', $translate);
public function routeStartup(\Zend_Controller_Request_Abstract $request)
$locale = new Zend_Locale('pl_PL'); //default locale
if (preg_match('/\/([a-z]2)([\/].*)/', $request->getRequestUri(),$matches)) //if locale is found in request
$lang = $matches[1]; //obtain locale
/* @var $locale Zend_Locale */
switch ($lang)
case 'en':
$locale->setLocale('en_GB');
break;
Zend_Registry::set('Zend_Locale',$locale);
$this->initTranslator($locale);
$router = Zend_Controller_Front::getInstance()->getRouter();
/* @var $router Zend_Controller_Router_Rewrite */
$langRoute = new Zend_Controller_Router_Route(
':lang',
array(
'lang' => $lang
),
array(
'lang' => '[a-z]0,2'
)
);
$routes = $router->getRoutes();
foreach ($routes as $name=>$route)
if ($name != 'default')
/* @var $route Zend_Controller_Router_Route */
$router->removeRoute($name);
/* @var $lang Zend_Controller_Router_Route */
$chain = new Zend_Controller_Router_Route_Chain();
$chain->chain($langRoute)->chain($route);
$router->addRoute($name,$chain);
$router->route($request);
else
$this->initTranslator($locale);
parent::routeStartup($request);
【讨论】:
以上是关于在 Zend 中使用多语言路由器的主要内容,如果未能解决你的问题,请参考以下文章
如何在多语言 asp.net MVC 网站的 URL 中支持有语言和无语言的路由