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

时间:2014-10-21 18:10:59

标签: php zend-framework zend-framework2

如何在url中包含en,tr,...等当前语言环境。你可以在symfony或Joomla看到这样的。 当使用时输入此网址:http://test.com这会自动转换为http://test.com/en我想在网址中包含当前区域设置并识别它并更改网站区域设置。

1 个答案:

答案 0 :(得分:0)

在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}',
            ),
        ),
    ),

所有其他路线都应该是这条路线的孩子。

相关问题