在Zend中使用多语言路由器

时间:2011-06-11 01:25:35

标签: php zend-framework routes modular

我正在开发一个带有两个模块(admin和public)的多语言Zend应用程序,我想在url中传递语言代码,所以在我的bootstrap中我有:

受保护的功能_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);

}

事情是,即使我没有指定语言,我也希望它能够工作。我该怎么做?如何在_initLocale

中提取语言代码(en)以便使用它

提前致谢

3 个答案:

答案 0 :(得分:1)

这是我用于路由的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);
}

答案 1 :(得分:0)

您需要链接语言路线中的所有路线。见这里:

http://robertbasic.com/blog/chaining-routes-in-zend-framework/

两个模块:

$defaultRoute = new Zend_Controller_Router_Route(  
        ':module/:controller/:action', 

答案 2 :(得分:0)

这是一个通用的插件,允许我们创建自己的路线而不记住语言。它还设置了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);
    }

}