管理
用户(authenticared)
我正在做的是当我的管理员通过user/login
登录时,管理员被重定向到"webapp/story"
的页面,并且向管理员显示我已经制作的sidebarwidget以及何时用户(经过身份验证)通过"user/login"
登录,该用户刚被重定向到index.php
。
在使用代码执行此操作后,当我的管理员记录时,它会显示错误:
"无法修改标题信息 - 已经发送的标题(输出 开始于 C:\瓦帕\ WWW \ emergency_response \保护\部件\视图\ admin.php的:73) "
我使用了下面给出的代码,并在modules/user/controller/logincontroller.php
中使用了此代码。
<?php
class LoginController extends Controller
{
public $defaultAction = 'login';
/**
* Displays the login page
*/
public function actionLogin()
{
if (Yii::app()->user->isGuest) {
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if($model->validate()) {
$this->lastViset();
//this is the code which i have used for the task i am doing.
$role = Rights::getAssignedRoles(Yii::app() -> user -> Id);
foreach ($role as $role)
$role -> name;
if ($role -> name == 'Admin'){
$this->widget('AdminWidget') && $this->redirect(array('story'));
}
else{
$this->redirect(Yii::app()->user->returnUrl);
}
}
}
// display the login form
$this->render('/user/login',array('model'=>$model));
} else
$this->redirect(Yii::app()->controller->module->returnUrl);
}
private function lastViset() {
$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
$lastVisit->lastvisit = time();
$lastVisit->save();
}
}
答案 0 :(得分:1)
你可以先试一下这个:
替换:
$this->widget('AdminWidget') && $this->redirect(array('story'));
要:
$this->widget('AdminWidget');
$this->redirect(array('story'));
但我不认为以上内容会有效,因为您会有重定向,$this->widget('AdminWidget');
的第一个语句在redirect
之后不会生效。
第二个想法是做一些改变,因为我认为你有逻辑错误。试试这个:
class LoginController extends Controller {
public $defaultAction = 'login';
/**
* Displays the login page
*/
public function actionLogin() {
if (Yii::app()->user->isGuest) {
$model = new UserLogin;
// collect user input data
if (isset($_POST['UserLogin'])) {
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if ($model->validate()) {
$this->lastViset();
//this is the code which i have used for the task i am doing.
$role = Rights::getAssignedRoles(Yii::app()->user->Id);
foreach($role as $role) { //Loop every role
if ($role->name == 'Admin'){ //If a role is Admin then redirect
$this->redirect(array('webapp/story'));
}
}
//If none of the roles were admin then redirect to a user view
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the login form
$this->render('/user/login', array('model'=>$model));
} else {
$this->redirect(Yii::app()->controller->module->returnUrl);
}
}
private function lastViset() {
$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
$lastVisit->lastvisit = time();
$lastVisit->save();
}
}
在WebappController
:
class WebappController extends Controller {
public function actionStory() {
$this->widget('AdminWidget'); //Maby you have to add this inside the view
//Other code
$this->render('webapp');
}
}