Skip to content

Commit a1cb21e

Browse files
authored
Add withCookies method to ResponseTrait (#60503)
1 parent ade5a87 commit a1cb21e

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

src/Illuminate/Http/RedirectResponse.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@ public function with($key, $value = null)
5151
return $this;
5252
}
5353

54-
/**
55-
* Add multiple cookies to the response.
56-
*
57-
* @param array $cookies
58-
* @return $this
59-
*/
60-
public function withCookies(array $cookies)
61-
{
62-
foreach ($cookies as $cookie) {
63-
$this->headers->setCookie($cookie);
64-
}
65-
66-
return $this;
67-
}
68-
6954
/**
7055
* Flash an array of input to the session.
7156
*

src/Illuminate/Http/ResponseTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ public function withCookie($cookie)
141141
return $this;
142142
}
143143

144+
/**
145+
* Add multiple cookies to the response.
146+
*
147+
* @param array $cookies
148+
* @return $this
149+
*/
150+
public function withCookies(array $cookies)
151+
{
152+
foreach ($cookies as $cookie) {
153+
$this->headers->setCookie($cookie);
154+
}
155+
156+
return $this;
157+
}
158+
144159
/**
145160
* Expire a cookie when sending the response.
146161
*

tests/Http/HttpResponseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public function testWithCookie()
8585
$this->assertSame('bar', $cookies[0]->getValue());
8686
}
8787

88+
public function testWithCookies()
89+
{
90+
$response = new Response;
91+
$this->assertCount(0, $response->headers->getCookies());
92+
$this->assertEquals($response, $response->withCookies([
93+
new Cookie('foo', 'bar'),
94+
new Cookie('baz', 'qux'),
95+
]));
96+
$cookies = $response->headers->getCookies();
97+
$this->assertCount(2, $cookies);
98+
$this->assertSame('foo', $cookies[0]->getName());
99+
$this->assertSame('bar', $cookies[0]->getValue());
100+
$this->assertSame('baz', $cookies[1]->getName());
101+
$this->assertSame('qux', $cookies[1]->getValue());
102+
}
103+
88104
public function testResponseCookiesInheritRequestSecureState()
89105
{
90106
$cookie = Cookie::create('foo', 'bar');

0 commit comments

Comments
 (0)