我是 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/