I have a custom parent class for all of my controllers:
class Controller extends CController
Then I created a method to help with the translation of my app, as below:
public static function t($str, $placeholders = array())
{
$category = strtolower(Yii::app()->getRequest()->getPathInfo());
return Yii::t($category, $str, $placeholders);
}
But, when I invoke it from within a view file, as below:
array('label'=>self::t('Home'), 'url'=>array('/site/index'))
I’ve got an error like this:
CException
Description
Property "SiteController.t" is not defined.
Source File
/var/www/yii/framework/base/CComponent.php(264)
00252: * @since 1.0.2
00253: */
00254: public function __call($name,$parameters)
00255: {
00256: if($this->_m!==null)
00257: {
00258: foreach($this->_m as $object)
00259: {
00260: if($object->getEnabled() && method_exists($object,$name))
00261: return call_user_func_array(array($object,$name),$parameters);
00262: }
00263: }
00264: if(class_exists('Closure', false) && $this->$name instanceof Closure)
00265: return call_user_func_array($this->$name, $parameters);
00266: throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',
00267: array('{class}'=>get_class($this), '{name}'=>$name)));
00268: }
00269:
00270: /**
00271: * Returns the named behavior object.
00272: * The name 'asa' stands for 'as a'.
00273: * @param string the behavior name
00274: * @return IBehavior the behavior object, or null if the behavior does not exist
00275: * @since 1.0.2
00276: */
Stack Trace
#0 /var/www/yii/framework/base/CComponent.php(264): CComponent->__get('t')
#1 /var/www/crm/protected/views/layouts/main.php(32): CComponent->__call('t', Array)
#2 /var/www/crm/protected/views/layouts/main.php(32): CBaseController->t('Home')
#3 /var/www/yii/framework/web/CBaseController.php(119): require('/var/www/crm/pr...')
#4 /var/www/yii/framework/web/CBaseController.php(88): CBaseController->renderInternal('/var/www/crm/pr...', Array, true)
#5 /var/www/yii/framework/web/widgets/CContentDecorator.php(78): CBaseController->renderFile('/var/www/crm/pr...', Array, true)
#6 /var/www/yii/framework/web/widgets/CContentDecorator.php(56): CContentDecorator->decorate('<div class="con...')
#7 /var/www/yii/framework/web/widgets/COutputProcessor.php(45): CContentDecorator->processOutput('<div class="con...')
#8 /var/www/yii/framework/web/CBaseController.php(199): COutputProcessor->run()
#9 /var/www/yii/framework/web/CBaseController.php(294): CBaseController->endWidget('CContentDecorat...')
#10 /var/www/crm/protected/views/layouts/column1.php(7): CBaseController->endContent()
#11 /var/www/yii/framework/web/CBaseController.php(119): require('/var/www/crm/pr...')
#12 /var/www/yii/framework/web/CBaseController.php(88): CBaseController->renderInternal('/var/www/crm/pr...', Array, true)
#13 /var/www/yii/framework/web/CController.php(741): CBaseController->renderFile('/var/www/crm/pr...', Array, true)
#14 /var/www/crm/protected/controllers/SiteController.php(74): CController->render('index', Array)
#15 /var/www/yii/framework/web/actions/CInlineAction.php(50): SiteController->actionIndex()
#16 /var/www/yii/framework/web/CController.php(300): CInlineAction->run()
#17 /var/www/yii/framework/web/filters/CFilterChain.php(133): CController->runAction(Object(CInlineAction))
#18 /var/www/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()
#19 /var/www/yii/framework/web/CController.php(1049): CFilter->filter(Object(CFilterChain))
#20 /var/www/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))
#21 /var/www/yii/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))
#22 /var/www/yii/framework/web/CController.php(283): CFilterChain->run()
#23 /var/www/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)
#24 /var/www/yii/framework/web/CWebApplication.php(324): CController->run('')
#25 /var/www/yii/framework/web/CWebApplication.php(121): CWebApplication->runController('')
#26 /var/www/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()
#27 /var/www/crm/index.php(16): CApplication->run()
#28 {main}
Does anyone knows what is happening?