在zend框架2(ZF2)的url路由中始终包括当前区域设置


always include current locale in url route in zend framework 2 (ZF2)

如何包含当前区域设置,如en、tr。。。在url中。你可以在symfony或Joomla中看到类似的情况。当使用enter访问此url时:http://test.com这会自动转换为http://test.com/en我想在url中包含当前区域设置,并识别它并更改网站区域设置。

在Module.php文件中:

public function onBootstrap(MvcEvent $e)
{                
    $em = StaticEventManager::getInstance();
    $em->attach('Zend'Mvc'Application', MvcEvent::EVENT_ROUTE, array($this, 'onRoute'), 
}
public function onRoute(MvcEvent $e)
{
    $routeLang = $e->getRouteMatch()->getParam('lang', false);
    if(!$routeLang)
        $e->getRouter()->setDefaultParam('lang', 'Locale::getDefault());
}

以及您的主要路线配置:

    'app' => array(
        'type' => 'Segment',
        'options' => array(
            'route' => '/[:lang]',
            'defaults' => array(
                'controller' => 'Application'Controller'Index',
                'action' => 'index',
            ),
            'constraints' => array(
                'lang' => '[a-z]{0,2}',
            ),
        ),
    ),

所有其他路由都应该是该路由的子路由。