有兩種方法,一種是兩個入口文件,另外一種能夠把後臺作爲一個模塊。
一,兩個入口文件方法:
項目目錄結構(圖):
先後臺引導文件分別以下:
// index.php:
require('path/to/yii.php');
Yii::app()->createWebApplication('protected/config/main.php')->run();
// admin.php:
require('path/to/yii.php');
Yii::app()->createWebApplication('protected/admin/config/main.php')->run();
這樣配置比較麻煩一些,咱們能夠採用覆蓋的方法,參考了一個貼子的方法,我又作了一些修改.
protected/admin/config/main.php的代碼以下:
php
<?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', // autoloading model and component classes 'import'=>array( 'application.models.*', 'application.components.*', 'application.extensions.*', 'application.extensions.nestedset.*', 'backend.models.*', 'backend.components.*', //這裏的前後順序必定要搞清 ), 'components'=>array( 'user'=>array( // enable cookie-based authentication 'allowAutoLogin'=>true, ), ), // main is the default layout //'layout'=>'main', // alternate layoutPath 'layoutPath'=>dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'layouts'.DIRECTORY_SEPARATOR, ); if(!function_exists('w3_array_union_recursive')) { /** * This function does similar work to $array1+$array2, * except that this union is applied recursively. * @param array $array1 - more important array * @param array $array2 - values of this array get overwritten * @return array */ 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,方法以下:
>yiic shell path/to/site/admin.php
>model Article
>crud Article
這樣後臺對應的controller和view就生成了!
若是隻對後臺的變量進行配置的話,只須要修改protected/admin下的配置文件就能夠了!
你們若是有不明白的地方,歡迎討論!
二,把後臺作爲一個模塊方法:
第一步:用GII會成一個admin的模塊;
第二步:打開(模塊名+Module.php)的文件,我這裏是oldweeklyadminModule.php文件進行編輯裏面有一個OldweeklyadminModule的類繼承於CWebModule首先咱們調用init的方法:
public function init()
{
parent::init();//這步是調用main.php裏的配置文件
//當Module建立時這個方法就會被調用
// 咱們能夠修改代碼來定製Module
// import the module-level models and components
$this->setImport(array(
'oldweeklyadmin.models.*',
'oldweeklyadmin.components.*',
));
//這裏重寫父類裏的組件
//若有須要還能夠參考API添加相應組件
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;
}
總結:最近用yii框架開發項目,遇到了不少問題,把遇到的這些問題記下來分享給你們,你們一塊兒討論和學習。
shell