Reputation: 348
I am very new in yii.
How can I set default home page in my site according to user role? what method is used in yii for this.
What i did is, in my index action i render index file. But how can I make it rolebased?
public function actionIndex() {
$this->render('index');
}
Any help?
Upvotes: 2
Views: 6422
Reputation: 504
You probably figured this out by now but didn't post your answer. Just in case this helps anyone, one implementation is as follows. I do not know if there is something built into RBAC that does this, but simple enough to implement.
In the file protected/controllers/SiteController.php change 1 line:
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));
}
and add this method to the same file
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..
}
}
What you redirect to could vary considerably depending on what type of page you want at the home page. In this case I use a static page. If your pages are named consistently you could leave out the switch statement and concatenate the role to the view name within the createURL.
Upvotes: 2
Reputation: 7896
You can change you view file in your default controller and action according to user type for example:
if($usertype == 'user_type1') { $this->render('usertypeview1'); }
if($usertype == 'user_type2') { $this->render('usertypeview2'); }
here usertypeview1 & usertypeview2 is the name of your view files under view folder.
Also you can change the layout as well according to your user type for example:
if($usertype == 'user_type1') { $this->layout = 'column1'; }
if($usertype == 'user_type2') { $this->layout = 'column2'; }
here column1 and column2 is the layout files under layout folder in views folder
I hope this will help you.
Upvotes: 2