ZF2 - 语言路线

ZF2 - Language Routes

我需要在 Zend Framework 2 中创建路由器配置,例如:

http://www.example.com/en
http://www.example.com/fr
http://www.example.com/es

然后像我的"cms"模块这样的模块路由必须匹配:

http://www.example.com/en/cms/test.html (english version)
http://www.example.com/fr/cms/test.html (french version)
http://www.example.com/es/cms/test.html (spanish version)

当然这个link必须匹配英语默认语言:

http://www.example.com/cms/test.html (english version)

我已经使用了 SimLocale 但没有成功,因为它也不能处理资产 links。所以我更喜欢通过一个简单的参数为我所有的自定义模块手动处理语言选择。

您可以为您需要的每种语言使用子域我想您不想或确实不需要(例如 /en)更改应用程序的语言and/or 网站。是否必须使用 /en、/fr 等?

这个问题今天已经在#zftalk IRC 上解决了(感谢 Michelangelo 根据我们向您解释的内容做出贡献...)。

要解决这个问题,我们必须看看定义的路由:

'router' => array(
    'routes' => array(
        'language' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '[/:lang]',
                'defaults' => array(
                    '__NAMESPACE__' => 'Cms\Controller',
                    'controller' => 'Index',
                    'action' => 'page',
                    'lang' => 'en',
                    'slug' => 'homepage'
                ),
                'constraints' => array(
                    'lang' => '[a-z]{2}'
                )
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'list' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/cms',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Cms\Controller',
                            'controller' => 'Index',
                            'action' => 'index',
                            'page' => 1
                        ),
                    ),
                ),
                'page' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '[/:slug].html',
                        'constraints' => array(
                            'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'action' => 'page',
                        ),
                    ),
                ),
                'search' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/search/[query/:query]',
                        'constraints' => array(
                            'query' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'action' => 'search',
                            'query' => null,
                        ),
                    ),
                ),
                'paginator' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/list/[page/:page]',
                        'constraints' => array(
                            'page' => '[0-9]*',
                        ),
                        'defaults' => array(
                            'page' => 1,
                        ),
                    ),
                ),
            ),
        ),

然后,问题的更新描述如下:

mikunos: http://www.example.com/en (homepage) this is ok

mikunos: http://www.example.com/en/cms (cms list) this is ok

mikunos: http://www.example.com/en/cms/about.html (page) this not

这导致我们将 'page' 路由更改为 'list' (/cms) 的子路由。

然后讨论结束,指出如果选择了 none,如何设置默认语言:

mikunos: as you have seen I need two main routes

mikunos: the first one is /lang/module and the second one is /module

mikunos: so I have to create two branches

mikunos: right?

tdutrion: you may want to use an url rewriting or something here

tdutrion: I would go for a redirect 301 from /module to /en/module so you have no duplicate content

tdutrion: and then no route can be accessed without a language

mikunos: ok

tdutrion: you can do it different ways, but I think the best one would be creating a event listener and do that before routing

tdutrion: you can also do it in your .htaccess of you are using apache, but then what if you migrate to IIS or Ngnix or else...

mikunos: interesting

tdutrion: again I believe Jurian Sluiman’s code does that