我是yii的新手。我希望我的管理员从webapp / user / login登录时重定向到我想要的页面localhost / webapp / story现在它将我重定向到index.php。
我还注册了一个用户,并为该用户提供了一个经过身份验证的角色,当我的用户(经过身份验证的用户)通过webapp / user / login登录时,我希望该用户被重定向到index.php。
所以有两件事:
1. redirecting admin to the desired page which is webapp/story.
2. redirecting the authenticated user to index.php.
我正在使用yii用户和正确的扩展名。请帮我解决一下这个。 代码LoginController如下:
<?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();
if (Yii::app()->user->returnUrl=='/index.php')
$this->redirect(Yii::app()->controller->module->returnUrl);
else// yehen par kuch aye ga according
$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)
我认为可能是这样的事情
<?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();
// Old code commentede
//if (Yii::app()->user->returnUrl=='/index.php')
// $this->redirect(Yii::app()->controller->module->returnUrl);
//else// yehen par kuch aye ga according
// $this->redirect(Yii::app()->user->returnUrl);
// new code
if (UserModule::isAdmin()){
$this->redirect(array('story/index'));
}
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();
}
}