Localization url路由中的zf2匹配语言

Localization url路由中的zf2匹配语言,localization,internationalization,url-routing,zend-framework2,Localization,Internationalization,Url Routing,Zend Framework2,如何将www.mydomain.com/en/aboutus等url匹配到 controller -> index action -> aboutus lang -> en 在zf2模块路由配置中 在zf1中,我们通过类似这样的方式来解决这个问题 $route = new Zend_Controller_Router_Route( '/contact/:lang', array( 'module' => 'default',

如何将www.mydomain.com/en/aboutus等url匹配到

controller -> index
action -> aboutus
lang -> en
在zf2模块路由配置中

在zf1中,我们通过类似这样的方式来解决这个问题

$route = new Zend_Controller_Router_Route(
    '/contact/:lang',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    )
);
但问题是另一回事
我们希望首先确定url中的语言,然后查看用户请求的控制器或操作

zf2在路由器中支持hirachy,这样您就可以像树一样构建路由

对于您的情况,您必须创建一个与url中的lang匹配的父路由

www.mydomain.com/en
www.mydomain.com/fa
www.mydomain.com/de

然后孩子们在里面为别人写路线

例如,代码:

'langroute' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/[:lang]',
                    'defaults' => array(
                        'lang' => 'en',
                    ),
                    'constraints' => array(
                        'lang' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'home' => array(
                        'type' => 'Zend\Mvc\Router\Http\Literal',
                        'options' => array(
                            'route' => '/',
                            'defaults' => array(
                                'controller' => 'Application\Controller\Index',
                                'action' => 'index',
                            ),
                        ),
                    ),
                    // The following is a route to simplify getting started creating
                    // new controllers and actions without needing to create a new
                    // module. Simply drop new controllers in, and you can access them
                    // using the path /application/:controller/:action
                    'aboutus' => array(
                        'type' => 'Zend\Mvc\Router\Http\Literal',
                        'options' => array(
                            'route' => '/aboutus',
                            'defaults' => array(
                                'controller' => 'Application\Controller\Index',
                                'action' => 'aboutus',
                            ),
                        ),
                    ),
),
正如你所看到的,兰格鲁特与恩德法或。。。朗文 然后检查内部页面的子路由
在本例中,url www.mydomain.com/en/匹配langen和路由home请将此代码添加到您的模块module.config.php

  return array(
    'router' => array(
        'routes' => array(
               // The following is a route to simplify getting started creating
                // new controllers and actions without needing to create a new
                // module. Simply drop new controllers in, and you can access them
                // using the path /Module_Name/:controller/:lang/:action
                'your_route_name_here' => array(
                    'type'    => 'Literal', 
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Module_Name\Controller',
                            'controller'    => 'Index',
                            'action'        => 'index',

                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'default' => array(
                            'type'    => 'Segment',
                            'options' => array(
                                'route'    => '/[:controller][/:lang][/:action]',
                                'constraints' => array(
                                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'lang'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                       'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                ),
                                'defaults' => array(
                                   'controller'    => 'Index',
                                   'lang'        => 'en',
                                   'action'        => 'index',
                                ),
                            ),
                        ),
                    ),
                ),

             ),
         ),
     );