我试图在yii框架中登录后重定向但是它无法正常工作我想在登录这里后重定向到客户/仪表板我正在使用的代码
public function actionLogin() {
if (Yii::app()->user->getId() !== null) {
$url = Yii::app()->createUrl('customer/dashboard');
$this->redirect($url);
// $this->redirect(Yii::app()->createUrl('customer/dashboard'));
}
$this->layout = "main";
$model = new LoginForm;
$this->title = "Login";
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()) {
// we are now redirecting to the home page after login instead (as
// in update note above). IF the user is not applying for a job.
$url = Yii::app()->createUrl('customer/dashboard');
$this->redirect($url);
// $this->redirect(Yii::app()->createUrl('customer/dashboard'));
} else {
$_SESSION['pageName'] = "/UserEvents/LoginError";
}
}
// display the login form
$this->render('login', array('model' => $model));
}
我正在使用actionLogin函数登录
问候
答案 0 :(得分:1)
将代码Yii::app()->createUrl('customer/dashboard');
更改为Yii::app()->urlManager->createUrl('customer/dashboard')
。您也可以将数组传递给redirect()
函数,Yii将负责生成正确的网址$this->redirect(['customer/dashboard']);
答案 1 :(得分:1)
如果您想使用createUrl函数,请遵循以下模式
$this->redirect(Yii::app()->createUrl('/modulename/controllername/action'));
如果您想在没有createUrl的情况下重定向,请使用下面的
$this->redirect(array('controllername/action'));
答案 2 :(得分:0)
使用此代码进行重定向。
if (Yii::app()->user->getId() !== null) {
$this->redirect(array("customer/dashboard"));
}