日期:2014-05-20 浏览次数:21016 次
public function resolveViewFile($viewName,$viewPath,$basePath,$moduleViewPath=null) { if(empty($viewName)) return false; if($moduleViewPath===null) $moduleViewPath=$basePath; if(($renderer=Yii::app()->getViewRenderer())!==null) $extension=$renderer->fileExtension; else $extension='.php'; if($viewName[0]==='/') { if(strncmp($viewName,'//',2)===0) $viewFile=$basePath.$viewName; else $viewFile=$moduleViewPath.$viewName; } else if(strpos($viewName,'.')) $viewFile=Yii::getPathOfAlias($viewName); else { $viewFileBak = $viewFile=$viewPath.DIRECTORY_SEPARATOR.$viewName; if (isset(Yii::app()->language)) { $viewFile=$viewPath.DIRECTORY_SEPARATOR.Yii::app()->language.DIRECTORY_SEPARATOR.$viewName; } } if(is_file($viewFile.$extension)) return Yii::app()->findLocalizedFile($viewFile.$extension); else if($extension!=='.php' && is_file($viewFile.'.php')) return Yii::app()->findLocalizedFile($viewFile.'.php'); else { if (isset($viewFileBak)) { if(is_file($viewFileBak.$extension)) return Yii::app()->findLocalizedFile($viewFileBak.$extension); else if($extension!=='.php' && is_file($viewFileBak.'.php')) return Yii::app()->findLocalizedFile($viewFileBak.'.php'); } } return false; }
/** * Abstract controller class for Theme&Language Controller.<br> * @author henry * */ abstract class TLController extends CController { /** * @var array context menu items. This property will be assigned to {@link CMenu::items}. */ public $menu=array(); /** * @var array the breadcrumbs of the current page. The value of this property will * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links} * for more details on how to specify this property. */ public $breadcrumbs=array(); public function init() { parent::init(); if ($this->hasEventhandler('onControllerInit')) { $this->onControllerInit(new CEvent($this)); } //$this->layout = '/layouts/'.Yii::app()->language.'/column1'; } /** * Supported befaviors * @return array */ public function behaviors() { return array_merge(parent::behaviors(), array( 'themeBehavior'=>array( 'class'=>'application.behaviors.ThemeBehavior', ), 'langBehavior'=>array( 'class'=>'application.behaviors.LangBehavior', ), )); } public function onControllerInit($event) { $this->raiseEvent('onControllerInit', $event); } }
//ThemeBehavior.php class ThemeBehavior extends CBehavior { const COOKIE_KEY = '__theme'; public function events() { return array_merge(parent::events(), array( 'onControllerInit'=>'controllerInit', )); } public function controllerInit($event) { $v = Yii::app()->request->getParam(self::COOKIE_KEY); if (!isset($v)) { $v = Yii::app()->request->cookies[self::COOKIE_KEY]; if (!isset($v)) { $v = Yii::app()->theme->name; } else { $v = $v->value; } } Yii::app()->theme = $v; Yii::app()->request->cookies[self::COOKIE_KEY] = new CHttpCookie(self::COOKIE_KEY, $v); } }
//LangBehavior.php class LangBehavior extends CBehavior { const COOKIE_KEY = '__lang'; private $_lang = ''; public function events() { return array_merge(parent::events(), array( 'onControllerInit'=>'controllerInit', )); } public function controllerInit($event) { $v = Yii::app()->request->getParam(self::COOKIE_KEY); if (!isset($v)) { $v = Yii::app()->request->cookies[self::COOKIE_KEY]; if (!isset($v)) { $v = Yii::app()->language; } else { $v = $v->value; } } Yii::app()->language = $v; Yii::app()->request->cookies[self::COOKIE_KEY] = new CHttpCookie(self::COOKIE_KEY, $v); } }