Zend Framework 2 - Zend \ Mvc \ Router \ Http \ Part - 模块配置

时间:2012-05-21 13:53:41

标签: zend-route zend-framework2 zend-framework-mvc zend-router

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

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

我完全理解如何使用DI

配置/[:namespace[/:controller[/:action]]]
http://localhost/application/index/index/
http://localhost/guestbook/index/index/
http://localhost/forum/index/index/

我不明白的是如何配置 Part 路由,它将成为所有路由的基础。在ZF1中,我使用路由链接来实现此目的。

所以我需要配置适用于网站范围的/[: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/

1 个答案:

答案 0 :(得分:2)

我认为你要找的是child_routes配置键。看一下ZfcUser configures it's routing (here):它创建一个基础文字路径(/ user),然后通过child_routes数组将子路径(/ user / login等)链接到它上。

我觉得这样的事情可以帮到你:

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

然后在你的控制器中你可以这样做以获得lang和locale:

$this->params()->fromRoute('lang');
$this->params()->fromRoute('locale');