ZF2中的默认语言路由

时间:2013-01-08 15:30:58

标签: routes zend-framework2

在Zend Framework 2应用程序中,我有两种语言,'nl'(默认)和'en'。请求'nl'的网址如下:

/controller/action

和'en'之类的:

/en/controller/action

首先,我想将默认语言路由/重写为:

/ NL /控制器/动作

以便能够随后使用段路径,如:

[:lang/[:controller/[:action]]]

我用下面的正则表达式尝试了这一点(前面是负面的)

'lang' => array(
    'type' => 'Zend\Mvc\Router\Http\Regex',
    'options' => array(
        'regex' => '/(?!en)(.*)',
        'spec'  => '/nl$2',
     ),
),

(此路线不应映射到控制器/操作,但只应将URL重写为新路径)

但我明白了:

Page not found.

The requested controller could not be mapped to an existing controller class.

什么是正确运作的路线?或者使用Web服务器重写是否更好?

2 个答案:

答案 0 :(得分:1)

路线应该是

/[:lang/[:controller/[:action]]]

答案 1 :(得分:0)

不需要正则表达式路由,下面也可以使用。语言段是可选的。在我的应用程序中,它只能是'en',如果省略,则默认为'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/]',
                            'constraints' => array(
                                'lang'       => 'en',
                            ),
                            'defaults' => array(
                                'lang' => 'nl',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
相关问题