我正在使用 ZF2 创建多语言应用程序..并且无法确定如何添加将构成每个 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/

我不明白的是如何配置 部分 路线将成为所有路线的基础..在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/
有帮助吗?

解决方案

我想你正在寻找的是 child_routes 配置键。看看如何 ZfcUser 配置其路由(此处):它创建一个基本的文字路由(/user),然后通过以下方式将子路由(/user/login等)链接到它上面: child_routes 大批。

我认为这样的事情会对你有用:

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

然后在你的控制器中你可以这样做来获取语言和区域设置:

$this->params()->fromRoute('lang');
$this->params()->fromRoute('locale');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top