Yii Framework结合Theme、Language和Module
很长时间都在使用Yii做SNS游戏后台,其类导入、全局变量生命、对象自动实例化、Behavior、Event都十分好用。
最近研究了一下其WEB方面都东西,主要从Theme、Language、Module方面入手。
通常一个Yii的app目录结构如下:
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;}
2、在protected/components里新增TLController.php
/** * 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.phpclass 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.phpclass 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);}}