将后台模板整合到框架中有两种方法,一种是两个入口文件,另一种可以把后台做为一个模块。
一,两个入口文件方法: 项目目录结构(图): 前后台引导文件分别如下: index.php:
1 2 require (‘path/to/yii.php’); Yii::app()->createWebApplication(‘protected /config/main.php’)->run();
admin.php:
1 2 require (‘path/to/yii.php’); Yii::app()->createWebApplication(‘protected /admin/config/main.php’)->run();
这样配置比较麻烦一些,我们可以采用覆盖的方法,参考了一个贴子的方法,我又做了一些修改. protected/admin/config/main.php的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 <?php $backend =dirname(dirname(__FILE__ )); $frontend =dirname($backend ); Yii::setPathOfAlias(‘backend’, $backend ); $frontendArray =require ($frontend .’/config/main.php’);$backendArray =array ( ‘name’=>’网站后台管理系统’, ‘basePath’ => $frontend , ‘controllerPath’ => $backend .’/controllers’, ‘viewPath’ => $backend .’/views’, ‘runtimePath’ => $backend .’/runtime’, ‘import’=>array ( ‘application.models.*’, ‘application.components.*’, ‘application.extensions.*’, ‘application.extensions.nestedset.*’, ‘backend.models.*’, ‘backend.components.*’, ), ‘components’=>array ( ‘user’=>array ( ‘allowAutoLogin’=>true , ), ), ‘layoutPath’=>dirname(dirname(__FILE__ )).DIRECTORY_SEPARATOR.’views’.DIRECTORY_SEPARATOR.’layouts’.DIRECTORY_SEPARATOR, ); if (!function_exists(‘w3_array_union_recursive’)) { function w3_array_union_recursive ($array1 ,$array2 ) { $retval =$array1 +$array2 ; foreach ($array1 as $key =>$value ) { if (is_array($array1 [$key ]) && is_array($array2 [$key ])) $retval [$key ]=w3_array_union_recursive($array1 [$key ],$array2 [$key ]); } return $retval ; } } return w3_array_union_recursive($backendArray ,$frontendArray );
这里我们的model是公用的,controller和view是分开的,我们还可以通过命令行对后台进行model和crud,方法如下:
1 2 3 yiic shell path/to/site/admin.php model Article crud Article
这样后台对应的controller和view就生成了!如果只对后台的变量进行配置的话,只需要修改protected/admin下的配置文件就可以了!
二,把后台做为一个模块方法: 第一步:用GII会成一个admin的模块; 第二步:打开(模块名+Module.php)的文件,我这里是oldweeklyadminModule.php文件进行编辑里面有一个OldweeklyadminModule的类继承于CWebModule首先我们调用init的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 public function init ( ) { parent ::init(); $this ->setImport(array ( ‘oldweeklyadmin.models.*’, ‘oldweeklyadmin.components.*’, )); Yii::app()->setComponents(array ( ‘errorHandler’=>array ( ‘class ’=>’CErrorHandler ’, ‘errorAction ’=>’oldweeklyadmin /default /error ’, ), ‘user ’=>array ( ‘class ’=>’CWebUser ’, ‘stateKeyPrefix ’=>’oldweeklyadmin ’, ‘loginUrl ’=>Yii ::app ()->createUrl (‘oldweeklyadmin /default /login ’), ), ), false ); $this ->generatorPaths []=’oldweeklyadmin .generators ’; $this ->controllerMap =$this ->findGenerators (); } 如果进入module 的时候要进行密码验证并且和前台登录分开 就要进行设置对执行的动作进行识别 public function beforeControllerAction ($controller , $action ) { if (parent ::beforeControllerAction($controller , $action )) { $route =$controller ->id.’/’.$action ->id; if (!$this ->allowIp(Yii::app()->request->userHostAddress) && $route !==’default /error ’) throw new CHttpException(403 ,"You are not allowed to access this page." ); $publicPages =array ( ‘default /login’, ‘default /error ’, ); if (Yii::app()->user->isGuest && !in_array($route ,$publicPages )) Yii::app()->user->loginRequired(); else return true ; } return false ; } protected function allowIp ($ip ) { if (empty ($this ->ipFilters)) return true ; foreach ($this ->ipFilters as $filter ) { if ($filter ===’*’ || $filter ===$ip || (($pos =strpos($filter ,’*’))!==false && !strncmp($ip ,$filter ,$pos ))) return true ; } return false ; } 设置资源文件路径 public function getAssetsUrl ( ) { if ($this ->_assetsUrl===null ) $this ->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias(‘oldweeklyadmin.assets’)); return $this ->_assetsUrl; } public function setAssetsUrl ($value ) { $this ->_assetsUrl=$value ; }