问题描述:fecmall后台使用了https的配置,后台登陆账户后,无法进行跳转
问题追踪:fecmall的登陆部分,帐号登陆成功,但是就是无法跳转,因此我们去debug进行跳转的函数,查看到底是哪里导致的这个问题。
1.提交表单后:是这个函数:
https://github.com/fecshop/yii2_fecshop/blob/master/app/appadmin/modules/Fecadmin/controllers/LoginController.php#L39
public function actionIndex()
{
$langCode = Yii::$app->request->get('lang');
if ($langCode) {
Yii::$service->admin->setCurrentLangCode($langCode);
}
$isGuest = Yii::$app->user->isGuest;
//echo $isGuest;exit;
if(!$isGuest){
//$this->redirect("/",200);
Yii::$app->getResponse()->redirect("/")->send();
return;
}
$errors = '';
$loginParam = Yii::$app->request->post('login');
if($loginParam){
$AdminUserLogin = new AdminUserLogin;
$AdminUserLogin->attributes = $loginParam;
if($AdminUserLogin->login()){
//\fecadmin\helpers\CSystemlog::saveSystemLog();
Yii::$service->admin->systemLog->save();
//$this->redirect("/",200)->send();
Yii::$app->getResponse()->redirect("/")->send();
return;
}else{
$errors = CModel::getErrorStr($AdminUserLogin->errors);
}
}
return $this->render('index',[
'error' => $errors,
]);
}
也就是使用:Yii::$app->getResponse()->redirect("/")->send();
进行的跳转,
2.@yii/web/Response.php
https://github.com/yiisoft/yii2/blob/master/framework/web/Response.php#L872
通过debug,发现 if ($request->getIsAjax()) {
返回 true
https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L488
public function getIsAjax()
{
$origin = $this->headers->get('Origin');
return
($this->headers->get('X-Requested-With') === 'XMLHttpRequest') ||
($this->headers->get('Sec-Fetch-Mode') === 'cors') ||
($this->headers->get('Sec-Fetch-Site') === 'cross-site') ||
($origin !== null && $origin !== $this->getHostInfo());
}
通过打印发现 $origin
是https
开头的url,
$this->getHostInfo()
返回的是http
开头的url, 进入返回true
3.那么问题定位到了,为什么https的url,$this->getHostInfo()
返回的是http
开头的url?
最终解决:nginx配置问题
解决办法:在nginx配置中加入fastcgi_param HTTPS 'on';
然后重启nginx即可解决。