php - 如何在yii中设置默认主页?

标签 php yii

我是 yii 的新手。

如何根据用户角色设置网站的默认主页? yii 中使用什么方法来实现此目的。

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

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

有什么帮助吗?

最佳答案

您现在可能已经弄清楚了这一点,但没有发布您的答案。为了防止这对任何人有帮助,一种实现如下。我不知道 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 中的 View 名称。

关于php - 如何在yii中设置默认主页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206517/

相关文章:

php - 根据用户的时间设置问候语(早上好,下午好……)

php - Yii 获取当前登录用户

php - Zend Framework 2 中的服务定位器

php - Laravel 5.1 Ajax 代码 500

php - mysql 选择具有相同 id 的行并按另一列排序

php - 计算有多少笔付款是通过 cashondelivery、paypal、payu 等方式进行的?

php - yii中view.php和_view.php的区别

javascript - 在 Yii 中将 js 变量传递给模态

php在验证表单返回false时保留文件字段的值

php - 确保 http(s) 请求来 self 的 iOS 应用