Laravel:Cookie :: forget()不会删除Cookie,而是将值设置为null

时间:2020-05-05 18:39:51

标签: laravel

我具有检查特定Cookie的功能。如果它存在并且具有一定的价值,那么该用户将被重定向到其他地方,并且一旦他们被重定向,就应该忘记该cookie。

以下是在我的控制器上处理此问题的功能:

 /**
     * Set plan intent either to query param or cookie
     *
     * @return void
     */
    private function setPlanIntentCookie() {
        $this->plan_intent = Cookie::get('rh_plan_intent');
    }


    /**
     * Returns all the data needed for the paywall
     */
    public function paywall() {
        $this->setPlanIntentCookie();

        if ($this->plan_intent == "free") {

            $account = auth()->user()->accounts->first();
            $account->update([
                'trial_ends_at' => now()->addDays(30),
            ]);

            return redirect()->route('home')
                ->withCookie(Cookie::forget('rh_plan_intent'));

        }

     // rest of code isn't reached during this test
     ...

}

我的测试:

/**
     * @test
     */
    public function a_user_with_free_trial_cookie_will_skip_paywall_and_begin_trial() {
        $this->disableCookieEncryption();
        $this->actingAs($this->user);

        $this->withCookie('rh_plan_intent', 'free')
            ->get(route('paywall'))
            ->assertRedirect(route('home'))
            ->assertCookieMissing('rh_plan_intent');


        $this->account->refresh();
        $this->assertNotNull($this->account->trial_ends_at);

    }

这失败了

Cookie [rh_plan_intent] is present on response.
Failed asserting that Symfony\Component\HttpFoundation\Cookie Object &0000000064bec37e0000000044e2323c (
    'name' => 'rh_plan_intent'
    'value' => null
    'domain' => null
    'expire' => 1431023770
    'path' => '/'
    'secure' => null
    'httpOnly' => true
    'raw' => false
    'sameSite' => null
    'secureDefault' => false
) is null.

因此Cookie :: forget()似乎正在将值从free更改为null吗?我想完全删除Cookie。

编辑:我刚刚检查了laravel的源代码以及forget的作用:

public function forget($name, $path = null, $domain = null)
    {
        return $this->make($name, null, -2628000, $path, $domain);
    }

因此,它只会使该值成为null。如何删除?

0 个答案:

没有答案