【问题标题】:Routing Zend Framework 2 Language in url在 url 中路由 Zend Framework 2 语言
【发布时间】:2013-05-17 11:56:02
【问题描述】:

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

  • 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%',
                        ),
                    ),
                ),
            ),
        ),
    ),

【问题讨论】:

    标签: url parameters routes zend-framework2 translation


    【解决方案1】:

    我认为你的正则表达式需要

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

    使用 Segment 路由和适当的约束也可以这样做...

    '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)?',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 2017-06-09
      • 2014-05-02
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多