2012-11-03 23 views
2

我在yii很新。如何在yii中設置默認主頁?

如何根據用戶角色在我的網站中設置默認主頁?在這方面yii使用了什麼方法。

我所做的是,在我的索引操作中,我渲染索引文件。但我怎樣才能使其基於角色?

public function actionIndex() { 
    $this->render('index'); 
} 

任何幫助?

回答

3

你可能現在想通了,但沒有發佈你的答案。以防萬一這有助於任何人,一個實施如下。我不知道是否有內置RBAC的內容可以實現這一點,但足夠簡單。

在保護/控制器/ SiteController.php變化1行的文件:

public function actionLogin() 
{ 
    $model=new LoginForm; 

    // if it is ajax validation request 
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') 
    { 
     echo CActiveForm::validate($model); 
     Yii::app()->end(); 
    } 

    // collect user input data 
    if(isset($_POST['LoginForm'])) 
    { 
     $model->attributes=$_POST['LoginForm']; 
     // validate user input and redirect to the previous page if valid 
     if($model->validate() && $model->login()) 
      //$this->redirect(Yii::app()->user->returnUrl); change this 
         $this->roleBasedHomePage(); //to this 
    } 
    // display the login form 
    $this->render('login',array('model'=>$model)); 
} 

這種方法添加到同一個文件

protected function roleBasedHomePage() { 
    $role='theusersrole' //however you define your role, have the value output to this variable 
    switch($role) { 
     case 'admin': 
      $this->redirect(Yii::app()->createUrl('site/page',array('view'=>$role.'homepage')); 
     break; 
     case 'member': 
      $this->redirect(Yii::app()->createUrl('site/page',array('view'=>$role.'homepage')); 
     break; 
     //etc.. 
    } 
} 

您重定向怎麼可能有很大的不同取決於什麼在主頁上您想要的頁面類型。在這種情況下,我使用靜態頁面。如果您的頁面一致地命名,則可以省略switch語句並將角色連接到createURL中的視圖名稱。

2

你可以改變你在默認的控制器和根據用戶類型,例如查看文件:

if($usertype == 'user_type1') { $this->render('usertypeview1'); } 
if($usertype == 'user_type2') { $this->render('usertypeview2'); } 

這裏usertypeview1 & usertypeview2是你的視圖文件的視圖文件夾下的名稱。

if($usertype == 'user_type1') { $this->layout = 'column1'; } 
if($usertype == 'user_type2') { $this->layout = 'column2'; } 

這裏列1和列2的佈局文件夾下的佈局文件在views文件夾

我希望這將有助於:

您也可以按照例如您的用戶類型更改佈局以及您。