【问题标题】:Yii2 blank page on redirectYii2重定向时的空白页
【发布时间】:2018-09-21 16:06:18
【问题描述】:

我有几个使用重定向的操作,但在转换到新服务器后,所有重定向现在都会导致空白页面。我在日志中没有收到任何错误,我已经尝试了这个问题中的建议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(),
        ]);

    }

【问题讨论】:

    标签: php redirect yii


    【解决方案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),在这种情况下应该没问题。

    【讨论】:

      【解决方案2】:

      避免数组

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

      并最终使用 url::to

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

      确保您实际上需要 405(不允许使用 405 方法)而不是(302 Found = 默认)

      【讨论】:

      • 即使使用return $this-&gt;redirect(Url::to(['/site/subscription']), 405); 仍然会导致空白页。我确实更喜欢这样,所以我将它保留在代码中,但这并不能解决问题。
      • 我已经尝试过使用header(...),它也会导致空白页
      • 我已经检查过了,订阅页面从来没有被请求过
      • 好的,我今晚会添加它
      • 我添加了订阅操作
      猜你喜欢
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2020-02-10
      • 2021-02-10
      • 2016-03-25
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多