Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect withs #2939

Merged
merged 5 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified admin/release-userguide
100644 → 100755
Empty file.
59 changes: 59 additions & 0 deletions system/HTTP/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,65 @@ public function with(string $key, $message)
return $this;
}

/**
* Copies any cookies from the global Response instance
* into this RedirectResponse. Useful when you've just
* set a cookie but need ensure that's actually sent
* with the response instead of lost.
*
* @return $this|RedirectResponse
*/
public function withCookies()
{
$cookies = service('response')->getCookies();

if (empty($cookies))
{
return $this;
}

foreach ($cookies as $cookie)
{
$this->setCookie(
$cookie['name'],
$cookie['value'],
$cookie['expires'],
$cookie['domain'],
$cookie['path'],
'', // prefix
$cookie['secure'],
$cookie['httponly']
);
}

return $this;
}

/**
* Copies any headers from the global Response instance
* into this RedirectResponse. Useful when you've just
* set a header be need to ensure its actually sent
* with the redirect response.
*
* @return $this|RedirectResponse
*/
public function withHeaders()
{
$headers = service('response')->getHeaders();

if (empty($headers))
{
return $this;
}

foreach ($headers as $name => $header)
{
$this->setHeader($name, $header->getValue());
}

return $this;
}

/**
* Ensures the session is loaded and started.
*
Expand Down
10 changes: 10 additions & 0 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,16 @@ public function deleteCookie(string $name = '', string $domain = '', string $pat
return $this;
}

/**
* Returns all cookies currently set.
*
* @return array
*/
public function getCookies()
{
return $this->cookies;
}

/**
* Actually sets the cookies.
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/system/HTTP/RedirectResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,38 @@ public function testRedirectRouteBaseUrl()

Config::reset();
}

public function testWithCookies()
{
$_SESSION = [];

$baseResponse = service('response');
$baseResponse->setCookie('foo', 'bar');

$response = new RedirectResponse(new App());
$this->assertFalse($response->hasCookie('foo', 'bar'));

$response = $response->withCookies();

$this->assertTrue($response->hasCookie('foo', 'bar'));
}

public function testWithHeaders()
{
$_SESSION = [];

$baseResponse = service('response');
$baseResponse->setHeader('foo', 'bar');

$response = new RedirectResponse(new App());
$this->assertFalse($response->hasHeader('foo'));

$response = $response->withHeaders();

foreach ($baseResponse->getHeaders() as $name => $header)
{
$this->assertTrue($response->hasHeader($name));
$this->assertEquals($header->getValue(), $response->getHeader($name)->getValue());
}
}
}
9 changes: 4 additions & 5 deletions user_guide_src/source/changelogs/next.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ Release Date: Not released

**Next release of CodeIgniter4**

Enhancements
------------

The list of changed files follows, with PR numbers shown.


PRs merged:
-----------
- Two new functions were added to RedirectResponse, ``withHeaders()`` and ``withCookies()`` that copy the current
cookies and headers from the global Response instance.
8 changes: 7 additions & 1 deletion user_guide_src/source/general/common_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,16 @@ Miscellaneous Functions
// Set a flash message
return redirect()->back()->with('foo', 'message');

// Copies all cookies from global response instance
return redirect()->back()->withCookies();

// Copies all headers from the global response instance
return redirect()->back()->withHeaders();

When passing a URI into the function, it is treated as a reverse-route request, not a relative/full URI, treating
it the same as using redirect()->route()::

// Go to a named/reverse-routed URI
// Go to a named/reverse-routed URI
return redirect('named_route');

.. php:function:: remove_invisible_characters($str[, $urlEncoded = TRUE])
Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,11 @@ The methods provided by the parent class that are available are:
Example::

$cookie = $response->getCookie($name);

.. php:method:: getCookies()

:rtype array

Returns all cookies currently set within the Response instance.
These are any cookies that you have specifically specified to set during the current
request only.