2013-11-20 61 views
0

我需要做的是延长getPageTitle()功能,使标题被翻译和actioncontroller交换 这是我放在Controller.php这样Yii的扩展控制器功能

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; 
      } 
    } 
} 

功能参考从这里开始:http://www.yiiframework.com/forum/index.php/topic/22258-internationalisationtranslation-of-page-title/

但如果在视图中我设置一个新的自定义标题与这些

$this->pageTitle = 'Title'; 
    $this->setPageTitle('Title2'); 
    Yii::app()->getController()->pageTitle="Title3"; 
什么

标题没有得到改变

如果我检查parent::getPageTitle(),它总是返回值

如果我检查$this->_pageTitle它总是返回null

回答

0

我不认为这是一个私人问题,因为私有财产在这个类只引用。我认为你已经假定当pageTitle被引用为Controller属性时,getPageTitle()将被调用。但是这可能只适用于模型/主动记录。

如果外部代码引用了propertyName,并且它不能公开访问,则调用一个名为get + PrivatePropertyName的函数,我不认为它在Controller中是自动的。你必须编写你自己的__get()存取器来自动调用这个函数。

(在你引用的链接中,我只看到了明确调用getter和setter的例子。)