在url中路由Zend Framework 2语言 [英] Routing Zend Framework 2 Language in url

查看:87
本文介绍了在url中路由Zend Framework 2语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用翻译,我想使用一种语言结构,例如:

  • site.com(英语)
  • site.com/de/(德语)
  • site.com/fr/(法国)
  • site.com/nl/(荷兰语)

等.

我可以轻松地使用Literal中的路由器选项将其设置为"[a-z] {2}",但是我想排除不支持的语言,例如site.com/it/(如果不受支持)我想要404.我尝试使用正则表达式(添加受支持的语言)修复此问题,但某些错误(我不知道)出错了.

提前谢谢!

'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'language' => array(
                        'type'    => 'Regex',
                        'options' => array(
                            'regex'    => '/(?<lang>(de|fr|nl))?',
                            'defaults' => array(
                                'lang' => 'en', //default
                            ),
                            'spec' => '/%lang%',
                        ),
                    ),
                ),
            ),
        ),
    ),

解决方案

我认为您的正则表达式必须是

'regex'    => '/(?<lang>(de|fr|nl)?)'

您可以使用细分路线和适当的约束条件...

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'language' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route' => '[/:lang]',
                        'defaults' => array(
                            'lang' => 'en', //default
                        ),
                        'constraints' => array(
                            'lang' => '(en|de|fr|nl)?',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

For my application translation I would like to use a language structure e.g.:

  • site.com (english)
  • site.com/de/ (german)
  • site.com/fr/ (france)
  • site.com/nl/ (dutch)

etc..

I can do this easily with the router option in Literal as '[a-z]{2}' but I want to exclude languages I do not support, e.g. site.com/it/ if it is not supported I want a 404. I tried to fix this with regex (adding supported languages) but something (I do not know) went wrong.

Thanks in advance!

'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'language' => array(
                        'type'    => 'Regex',
                        'options' => array(
                            'regex'    => '/(?<lang>(de|fr|nl))?',
                            'defaults' => array(
                                'lang' => 'en', //default
                            ),
                            'spec' => '/%lang%',
                        ),
                    ),
                ),
            ),
        ),
    ),

解决方案

I think your regex needs to be

'regex'    => '/(?<lang>(de|fr|nl)?)'

You could so the same with a Segment route and an appropriate constraint...

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'language' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route' => '[/:lang]',
                        'defaults' => array(
                            'lang' => 'en', //default
                        ),
                        'constraints' => array(
                            'lang' => '(en|de|fr|nl)?',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

这篇关于在url中路由Zend Framework 2语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆