我正在开发一个包含两个模块(admin 和 public)的多语言 Zend 应用程序,我想在 url 中传递语言代码,所以在我的 Bootstrap 中我有:
protected 函数 _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
中使用它?
提前致谢
最佳答案
这是我用于路由的 bootstrap 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);
}
关于php - 在 Zend 中使用多语言路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6313493/