4

我想知道在 Yii 框架中实现“国际化(i18n)”和“动态 URL 管理”的更好方法。

一个(难以维护的)临时解决方案:

// protected/config/main.php
'language' => 'es',
...
'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName' => false,
        'rules'=>array(
            // pages
            'es/turismo/<slug:>' => array('visit/page', 'defaultParams' => array('lang' => 'es'), 'urlSuffix' => '.html'),
            'it/visita/<slug:>' => array('visit/page', 'defaultParams' => array('lang' => 'it'), 'urlSuffix' => '.html'),
            'en/travel/<slug:>' => array('visit/page', 'defaultParams' => array('lang' => 'en'), 'urlSuffix' => '.html'),
            'turismo/<slug:>' => array('visit/page', 'urlSuffix' => '.html'),

            // home
            'es/turismo' => array('visit/index', 'defaultParams' => array('lang' => 'es'), 'urlSuffix' => '.html'),
            'it/visita' => array('visit/index', 'defaultParams' => array('lang' => 'it'), 'urlSuffix' => '.html'),
            'en/travel' => array('visit/index', 'defaultParams' => array('lang' => 'en'), 'urlSuffix' => '.html'),

            // contact us
            'es/contactenos' => array('site/contact', 'defaultParams' => array('lang' => 'es'), 'urlSuffix' => '.html'),
            'it/contattaci' => array('site/contact', 'defaultParams' => array('lang' => 'it'), 'urlSuffix' => '.html'),
            'en/contact-us' => array('site/contact', 'defaultParams' => array('lang' => 'en'), 'urlSuffix' => '.html'),

        ),
    ),
...


// protected/controllers/VisitController.php
...
public function actionIndex($lang = 'es'){
    Yii::app()->language = $lang;
    ...
}
public function actionPage($slug, $lang = 'es'){
    Yii::app()->language = $lang;
    ...
}
...

这意味着,每个控制器的新配置“urlManager”规则并始终为每个控制器操作传递 $lang 参数。一个活生生的例子:

montmartrebutte.com

我尝试了其他一些选项但没有成功:

Yii 框架论坛:动态 URL 管理器路由

谢谢!

4

1 回答 1

3

规则:

'<lang:(es|it|en)>/(turismo|visita|travel)/<slug:>' => array('visit/page', 'urlSuffix' => '.html'),

控制器:

class VisitController extends Controller
{
    public function actionPage($slug)
    {

    }
}

父控制器(如果您不使用模块):

class Controller extends CController
{
    public function beforeAction($action)
    {
        Yii::app()->language = Yii::app()->request->getQuery('lang', 'es');
        return parent::beforeAction($action);
    }
}
于 2012-08-08T08:51:12.200 回答