Skip to content

Commit b263ea5

Browse files
authored
Merge pull request #1300 from bcit-ci/redirect
Allow redirect with Query Vars from the current request.
2 parents 400777f + 23700ff commit b263ea5

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

system/HTTP/RedirectResponse.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ public function withInput()
139139
return $this;
140140
}
141141

142+
/**
143+
* Makes it so that the URL used for the redirect will include
144+
* the Query variables in the current request.
145+
*
146+
* Using the $options array, can specify either only certain
147+
* keys, or all except some vars.
148+
*
149+
* NOTE: Should be called after either to() or route()
150+
*
151+
* @param array $options
152+
*
153+
* @return \CodeIgniter\HTTP\RedirectResponse
154+
*/
155+
public function withQuery(array $options = [])
156+
{
157+
$queryVars = service('request')->uri->getQuery($options);
158+
159+
$url = $this->getHeaderLine('Location');
160+
161+
$this->setHeader('Location', $url.'?'.$queryVars);
162+
163+
return $this;
164+
}
165+
142166
/**
143167
* Adds a key and message to the session as Flashdata.
144168
*

system/HTTP/URI.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ public function getQuery(array $options = []): string
401401

402402
if (array_key_exists('except', $options))
403403
{
404+
if (! is_array($options['except']))
405+
{
406+
$options['except'] = [$options['except']];
407+
}
408+
404409
foreach ($options['except'] as $var)
405410
{
406411
unset($vars[$var]);
@@ -410,6 +415,11 @@ public function getQuery(array $options = []): string
410415
{
411416
$temp = [];
412417

418+
if (! is_array($options['only']))
419+
{
420+
$options['only'] = [$options['only']];
421+
}
422+
413423
foreach ($options['only'] as $var)
414424
{
415425
if (array_key_exists($var, $vars))

tests/system/HTTP/RedirectResponseTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,37 @@ public function testWith()
121121
$this->assertSame($response, $returned);
122122
$this->assertArrayHasKey('foo', $_SESSION);
123123
}
124+
125+
public function testRedirectWithQueryIncludesAllVars()
126+
{
127+
$this->request->uri->setQueryArray(['foo' => 'bar', 'bar' => 'baz']);
128+
$response = new RedirectResponse(new App());
129+
130+
$response = $response->to('http://example.com/foo')->withQuery();
131+
132+
$this->assertTrue($response->hasHeader('Location'));
133+
$this->assertEquals('http://example.com/foo?foo=bar&bar=baz', $response->getHeaderLine('Location'));
134+
}
135+
136+
public function testRedirectWithQueryExcept()
137+
{
138+
$this->request->uri->setQueryArray(['foo' => 'bar', 'bar' => 'baz']);
139+
$response = new RedirectResponse(new App());
140+
141+
$response = $response->to('http://example.com/foo')->withQuery(['except' => 'foo']);
142+
143+
$this->assertTrue($response->hasHeader('Location'));
144+
$this->assertEquals('http://example.com/foo?bar=baz', $response->getHeaderLine('Location'));
145+
}
146+
147+
public function testRedirectWithQueryOnly()
148+
{
149+
$this->request->uri->setQueryArray(['foo' => 'bar', 'bar' => 'baz']);
150+
$response = new RedirectResponse(new App());
151+
152+
$response = $response->to('http://example.com/foo')->withQuery(['only' => 'foo']);
153+
154+
$this->assertTrue($response->hasHeader('Location'));
155+
$this->assertEquals('http://example.com/foo?foo=bar', $response->getHeaderLine('Location'));
156+
}
124157
}

user_guide_src/source/general/common_functions.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ Miscellaneous Functions
244244
// Set a flash message
245245
return redirect()->back()->with('foo', 'message');
246246

247+
// Include Query vars from the current request
248+
return redirect()->back()->withQuery();
249+
250+
// Include only a specific query var from the current request
251+
return redirect()->back()->withQuery(['only' => 'foo']);
252+
return redirect()->back()->withQuery(['only' => ['foo', 'bar']]);
253+
254+
// Include all query vars except specified vars from the current request
255+
return redirect()->back()->withQuery(['except' => 'foo']);
256+
return redirect()->back()->withQuery(['except' => ['foo', 'bar']]);
257+
247258
.. php:function:: remove_invisible_characters($str[, $url_encoded = TRUE])
248259
249260
:param string $str: Input string

0 commit comments

Comments
 (0)