如果您想要更“自动”的东西,例如:
- 始终在 pageTitle 中显示“动作 + 控制器”。示例:查看用户、删除用户...
您可以像这样制作过滤器:(也适用于多语言!!!)
1- 在protected/components/中创建一个文件PageTitleFilter.php
class PageTitleFilter extends CFilter {
public $controller;
protected function preFilter($filterChain) {
// logic being applied before the action is executed
$this->controller->pageTitle = Yii::t('app', Yii::app()->controller->action->id) . ' ' . Yii::t('app', Yii::app()->controller->id);
return true; // false if the action should not be executed
}
protected function postFilter($filterChain) {
// logic being applied after the action is executed
}
}
2- 在您的控制器中:
class MyController extends Controller {
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
array(
'PageTitleFilter + view, create, update, delete, admin',
'controller' => $this
),
);
}
}
然后你把每个动作的翻译放在一个文件 protected/messages/es/app.php 中,比如:
return array(
'view'=>'ver',
'delete'='eliminar'
);
链接:http://www.yiiframework.com/doc/guide/1.1/es/topics.i18n#locale-and-language
如果你想改变默认的pageTitle,你可以做任何动作:
$this->pageTitle= 'My page title';
如果你不想要多语言,请移除 Yii::t('app') 函数!