我正在开发一个有两个模块(admin和public)的多语言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
中使用?
提前感谢
这是我的路由引导init。它包含了使用多语言路由的所有解决方案,并具有模块和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);
}
您需要从语言路由链接所有路由。请参阅此处:
http://robertbasic.com/blog/chaining-routes-in-zend-framework/
两个模块:
$defaultRoute = new Zend_Controller_Router_Route(
':module/:controller/:action',
这是一个通用的插件,允许我们在不记住语言的情况下创建自己的路由。它还设置了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);
}
}