Yii2 重定向上的空白页



我有几个操作使用重定向,但在过渡到新服务器后,所有重定向现在都会导致空白页。我在日志中没有收到任何错误,我已经尝试了这个问题中的建议 YII2重定向导致空白页

当我回显var_dump(headers_sent())时,它返回 false。Yii 调试日志也显示 405 状态码。以下是我的行动。

我甚至尝试使用header("Location: http://www.google.com")它也会导致空白页

public function actionDashboard()
    {
        if(strtotime(UserInfo::findOne(Yii::$app->user->Id)->active_until) < strtotime(date("Y-m-d H:i:s"))){
            Yii::$app->session->setFlash('warning', 'Please subscribe below.');
            return $this->redirect(['site/subscription'], 405);
        }
        $model = new Score();
        $deadlines = new EDeadlines();
        return $this->render('dashboard', [
            'deadlines' => $deadlines,
            'model' => $model,
        ]);
    }
 public function actionSubscription()
    {
        Stripe::setApiKey(Yii::$app->params['stripe_sk']);
        $userInfo = UserInfo::findOne(Yii::$app->user->Id);
        $userInfo->customer_id != NULL ? $customer = Customer::retrieve($userInfo->customer_id) : $customer = NULL;
        $userPayments = StripeInvoice::find()
            ->where('customer_id=:customer_id', [':customer_id' => $userInfo['customer_id']])
            ->orderBy(['date' => SORT_DESC])
            ->all();
        $redeem_ch = NULL;
        $customer != NULL ? $account_balance = $customer->account_balance : $account_balance = 0;

        if($account_balance <= -1000 && $userInfo->refund_redeemed == 0):
            $redeem_ch = StripeInvoice::find()->where(['refunded' => 0, 'customer_id' => $userInfo->customer_id])->one();
            $userInfo->redeem_charge = $redeem_ch->charge_id;
            $userInfo->save();
        endif;
        return $this->render('subscription', [
            'userInfo' => $userInfo,
            'customer' => $customer,
            'account_balance' => $account_balance,
            'userPayments' => $userPayments,
            'referral_count' => UserInfo::find()->where(['referrer_code' => $userInfo->your_referral_code])->count(),
        ]);
    }

您使用的状态代码不正确 - 405 不适用于重定向:

超文本传输协议 (HTTP) 405 Method Not Allowed响应状态代码指示服务器已知请求方法,但目标资源不支持该请求方法。

服务器必须在 405 响应中生成一个Allow标头字段,其中包含目标资源当前支持的方法的列表。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

应从方法调用中删除此状态:

return $this->redirect(['site/subscription']);

Yii 将使用临时重定向 ( 302 ),这在这种情况下应该没问题。

避免数组

return $this->redirect('site/subscription', 405);

并最终使用 url::to

 use yiihelpersUrl;
 .....
 return $this->redirect(Url::to(['/site/subscription'])', 405);
确保您实际上需要 405(

不允许 405 方法)而不是(302 找到 = 默认值)

最新更新