簡體   English   中英

如何在yii中設置默認主頁?

[英]How to set default home page in yii?

我在yii中很新。

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

我所做的是,在索引操作中,我渲染了索引文件。 但是如何使它基於角色?

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

有什么幫助嗎?

您可以根據用戶類型在默認控制器和操作中更改視圖文件,例如:

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

這里的usertypeview1&usertypeview2是您在view文件夾下的視圖文件的名稱。

您也可以根據用戶類型更改布局,例如:

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

這里column1和column2是視圖文件夾中layout文件夾下的布局文件

我希望這能幫到您。

您現在可能已經知道了,但是沒有發布答案。 以防萬一這對任何人都有幫助,一種實現如下。 我不知道RBAC內置了什么功能,但是執行起來很簡單。

在文件protected / controllers / 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中的視圖名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM