Michael St Clair
Michael St Clair

Reputation: 6625

Yii2 blank page on redirect

I have a couple actions that I use redirects on but after transitioning to a new server all redirects now result in blank pages. I am not getting any errors in the logs and I already tried the suggestions in this question YII2 redirect causes blank page

When I echo var_dump(headers_sent()) it returns false. The Yii debug log displays the 405 status code also. Below is my action.

I've tried even using header("Location: http://www.google.com") and it also results in a blank page

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(),
        ]);

    }

Upvotes: 1

Views: 1245

Answers (2)

rob006
rob006

Reputation: 22174

You're using incorrect status code - 405 is not for redirections:

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource.

The server MUST generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods.

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

You should remove this status from method call:

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

Yii will use temporary redirection (302) which should be fine in this case.

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

Avoid array

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

and eventually use url::to

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

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

Be sure you effectively need 405 (405 Method Not Allowed) instead of (302 Found = default)

Upvotes: 0

Related Questions