我有 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粉2311124372024-04-05 09:46:37
透過對此主題的評論解決 https://laracasts.com/discuss/channels/laravel/redirect-with-not-working?page=1&replyId=337923
我做了一個手動重定向,但也不起作用,所以我添加了 ->send();到最後,正如該線程所建議的那樣,這解決了它。我沒有用 return back(); 測試這個但它可能也適用。
成功的程式碼如下
return redirect()->to('/settings/subscription')->send();