我有 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; }
通过对此线程的评论解决 https://laracasts.com/discuss/channels/laravel/redirect-with-not-working?page=1&replyId=337923
我做了一个手动重定向,但也不起作用,所以我添加了 ->send();到最后,正如该线程所建议的那样,这解决了它。我没有用 return back(); 测试这个但它可能也适用。
成功的代码如下