Yii2重定向空白页面

时间:2016-02-18 23:58:00

标签: php redirect yii

我有一些操作,我使用重定向,但在转换到新服务器后,所有重定向现在导致空白页。我在日志中没有收到任何错误,我已经尝试过这个问题中的建议YII2 redirect causes blank page

当我回显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(),
        ]);

    }

2 个答案:

答案 0 :(得分:1)

您使用的状态码不正确-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),在这种情况下应该可以。

答案 1 :(得分:0)

避免数组

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

并最终使用url :: to

 use yii\helpers\Url;
 .....

 return $this->redirect(Url::to(['/site/subscription'])', 405);

确保您实际需要405(405 Method Not Allowed)而不是(302 Found = default)