Php 使用yii框架基于登录呈现不同的页面
我希望主页呈现取决于用户角色登录 目前我在protected/controllers/break中有此功能;但它会重定向到另一个页面Php 使用yii框架基于登录呈现不同的页面,php,yii,login,Php,Yii,Login,我希望主页呈现取决于用户角色登录 目前我在protected/controllers/break中有此功能;但它会重定向到另一个页面 protected function roleBasedHomePage() { $roles = Yii::app()->user->getState('roles'); //however you define your role, have the value output to this variable switch($ro
protected function roleBasedHomePage() {
$roles = Yii::app()->user->getState('roles'); //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'=>$roles.'homepage')));
break;
case 'b':
$this->redirect(Yii::app()->createUrl('site/page',array('view'=>$roles.'homepage')));
break;
case 'guest':
$this->redirect(Yii::app()->createUrl('site/page',array('view'=>'homepage')));
break;
//etc..
}
public function actionLogin()
{
$model = new LoginForm();
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
echo CActiveForm::validate($model, array('username', 'password', 'verify_code'));
Yii::app()->end();
}
if (isset($_POST['LoginForm'])) {
$model->attributes = $_POST['LoginForm'];
if ($model->validate(array('username', 'password', 'verify_code')) && $model->login()) {
Yii::app()->user->setFlash('success', 'Welcome ' . app()->user->name);
// $this->redirect(Yii::app()->user->returnUrl);
$this->roleBasedHomePage();
}
}
$this->render('login', array('model' => $model));
}
}
如果我想重定向页面,但我希望主页url相同,并且内容根据“角色”的不同而变化,那么这种方法是有效的
e、 g.如果用户是“admin”,则我希望呈现“adminhome”
我猜下面的函数与此有关
public function actionIndex()
{
$this->render('index');
}
如果您想更改页面内容,但保持页脚和页眉不变,我会使用Ajax作为选项 例如: 1-创建三个不同的视图:admin.php、b.php和guest.php 2-在控制器中创建三种不同的方法:renderAdmin()、renderB()和renderGuest()。每一个都将渲染相应的视图 3-在同一控制器中,定义特定角色和方法的访问规则,因此不允许来自无效用户的Ajax调用(如果有人看到您的代码并试图在您发送帖子的Ajax URL中调用该方法)。这是因为在“index.php”中使用的默认标题中,您将分配调用特定方法的JS脚本,该方法将返回视图,这就是您在“main”content div中呈现的内容 4-在login()中调用index方法 5-在index方法中,呈现“index”视图,并将路径传递给调用“adminview”的特定JS脚本 你也可以使用
HTH你可以很容易地做到这一点。首先为每个角色创建一个视图。然后在登录后将每个人重定向到您的主页,但检查他们的角色,并根据该角色的视图“renderPartial()”。比如:
switch($role){
case 'admin' :
$this->renderPartial('application.views.site._admin'); // view for admin
break;
case 'superUser':
$this->renderPartial('application.views.site._superUser');// view for super user
break;
将控制器中的索引操作更改为以下内容:
public function actionIndex()
{
$roles = Yii::app()->user->getState('roles');
$view = $this->getViewForRole($roles); // you have to implement this function
$this->render($view . '_index');
}
在视图文件夹中创建视图文件:admin_index.php、customer_index.php等