返回();不適用於 1 條路線,但適用於幾乎相同的路線
P粉098417223
P粉098417223 2024-04-04 17:34:09
0
1
303

我有 2 條路線,一條用於取消訂閱,一條用於恢復,除非我遺漏了某些內容,否則兩條路線除了名稱/功能/網址之外都是相同的。當我取消恢復訂閱時,我單擊 /resume url,它執行該功能,然後返回,一切都如此之快,似乎從未離開過頁面,然後它閃爍了成功訊息。

我的 /cancel url 只是轉到一個空白頁面(我很確定這是正確的,因為如果它正常工作,你永遠不會看到它)並且它執行取消功能,但永遠不會返回。當我使用後退按鈕手動返回時,它會閃爍成功訊息。只是不明白為什麼它不會像預期的那樣自行返回。除此之外,如果您還需要任何其他信息,請告訴我。

工作:

Route::get('/resume', function (Request $request) {
// $user = User::find(1);
$user = Auth::user();
$response = $user->subscription()->resume();
toastr()->success('Your subscription has been successfully resumed.', 'Success' , ['closeButton' => 'true','positionClass' => 'toast-top-center']);
return back();
});

不工作:

Route::get('/cancel', function (Request $request) {
// $user = User::find(1);
$user = Auth::user();
$response = $user->subscription()->cancel();
toastr()->success('Your subscription has been successfully cancelled.', 'Success' , ['closeButton' => 'true','positionClass' => 'toast-top-center']);
return back();
});

我覺得這些沒有必要,但為了以防萬一,這是我的訂閱控制器中的 2 個函數。

public function cancel(): self
    {
        $response = LemonSqueezy::api('DELETE', "subscriptions/{$this->lemon_squeezy_id}");

        $this->sync($response['data']['attributes']);

        return $this;
    }

    /**
     * Resume the subscription.
     */
    public function resume(): self
    {
        if ($this->expired()) {
            throw new LogicException('Cannot resume an expired subscription.');
        }

        $response = LemonSqueezy::api('PATCH', "subscriptions/{$this->lemon_squeezy_id}", [
            'data' => [
                'type' => 'subscriptions',
                'id' => $this->lemon_squeezy_id,
                'attributes' => [
                    'cancelled' => false,
                ],
            ],
        ]);

        $this->sync($response['data']['attributes']);

        return $this;
    }

P粉098417223
P粉098417223

全部回覆(1)
P粉231112437

透過對此主題的評論解決 https://laracasts.com/discuss/channels/laravel/redirect-with-not-working?page=1&replyId=337923

我做了一個手動重定向,但也不起作用,所以我添加了 ->send();到最後,正如該線程所建議的那樣,這解決了它。我沒有用 return back(); 測試這個但它可能也適用。

成功的程式碼如下

return redirect()->to('/settings/subscription')->send();
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!