0

我正在使用 ZF2 创建一个多语言应用程序.. 并且无法确定如何添加部分 URL,该部分 URL 将构成每个 URL 的基础,而不管模块如何。

http://localhost/en/us/application/index/index/

我完全了解如何/[:namespace[/:controller[/:action]]]使用 DI进行配置

http://localhost/application/index/index/
http://localhost/guestbook/index/index/
http://localhost/forum/index/index/

我不明白的是如何配置将成为所有路由基础的Part路由。在 ZF1 中,我使用Route Chaining来实现这一点。

所以我需要配置一个适用于站点范围的部分路由,/[:lang[/:locale]]然后让模块配置/[:namespace[/:controller[/:action]]]或任何其他必要的路由..

http://localhost/en/us/application/index/index/
http://localhost/zh/cn/application/index/index/
http://localhost/en/uk/forum/index/index/
4

1 回答 1

2

I think what you are looking for is the child_routes configuration key. Take a look at how ZfcUser configures it's routing (here): it creates a base Literal route (/user) and then chains the sub-routes (/user/login, etc) onto it via the child_routes array.

I think something like this will do the trick for you:

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

Then in your controller you could do this to get the lang and locale:

$this->params()->fromRoute('lang');
$this->params()->fromRoute('locale');
于 2012-06-27T12:58:46.647 回答