Php Yii扩展控制器功能

Php Yii扩展控制器功能,php,yii,extend,overriding,Php,Yii,Extend,Overriding,我需要做的是扩展getPageTitle()函数,以便翻译标题,并交换操作和控制器 这是我放在Controller.php中的函数 private $_pageTitle; public function getPageTitle() { if($this->_pageTitle!==null) { return Yii::t('wm', $this->_pageTitle); } else { $controller

我需要做的是扩展
getPageTitle()
函数,以便翻译标题,并交换
操作
控制器
这是我放在Controller.php中的函数

private $_pageTitle;
public function getPageTitle()
{
    if($this->_pageTitle!==null) {
            return Yii::t('wm', $this->_pageTitle);
    } else {
            $controller = Yii::t('wm', ucfirst(basename($this->getId())));

            if($this->getAction()!==null && strcasecmp($this->getAction()->getId(),$this->defaultAction)) {
                    $action = Yii::t('wm', ucfirst($this->getAction()->getId()));
                    return $this->_pageTitle=Yii::app()->name.' - '.Yii::t('wm', '{action} {controller}', array('{action}' => $action, '{controller}' => $controller));
            } else {
                    return $this->_pageTitle=Yii::app()->name.' - '.$controller;
            }
    }
}
此处的参考:

但是如果在视图中,我用这些东西设置了一个新的自定义标题

  $this->pageTitle = 'Title';
  $this->setPageTitle('Title2');
  Yii::app()->getController()->pageTitle="Title3";
标题不会更改

如果我选中
parent::getPageTitle()
它总是返回值


如果我选中
$this->\u pageTitle
它总是返回null

private
变量只能在创建它们的类中访问,在您的情况下是
Controller

您应该改用
protected
,因为这些变量也可以在从声明它的类继承的所有类中访问


更多信息:和

我不认为这是一个私有问题,因为私有属性只在这个类中引用。我想您已经假设当pageTitle被引用为控制器属性时,将调用getPageTitle()。但这可能只适用于model/activerecords

“如果外部代码引用propertyName并且它不可公开访问,则调用名为get+PrivatePropertyName的函数”不是PHP特性,我认为它在Controller中不是自动的。您必须编写自己的_uget()访问器才能自动调用此函数

(在您引用的链接中,我只看到显式调用getter和setter的示例。)