Skip to content

Commit f6cf11e

Browse files
authored
Add Forward Query String option (#9)
Added option to forward the query string when redirecting, defaulting to true to be greedy by default. Migration will default to false for existing redirects.
1 parent ff97ae8 commit f6cf11e

10 files changed

+58
-8
lines changed

.github/workflows/tests.yml

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ jobs:
1616
phpVersion: ['8.0', '8.1']
1717
winterRelease: ['develop']
1818
winterReleaseDir: ['develop']
19-
include:
20-
- phpVersion: '7.4'
21-
winterRelease: 'v1.1.9'
22-
winterReleaseDir: '1.1.9'
2319
fail-fast: false
2420
env:
2521
phpExtensions: mbstring, intl, gd, xml, sqlite
@@ -63,7 +59,7 @@ jobs:
6359
mv winter-${{ matrix.winterReleaseDir }}/* ./
6460
rmdir winter-${{ matrix.winterReleaseDir }}
6561
shopt -u dotglob
66-
cp config/cms.php config/testing/cms.php
62+
[ ! -f config/testing/cms.php ] && cp config/cms.php config/testing/cms.php
6763
mkdir -p plugins/winter
6864
mv redirect-plugin plugins/winter/redirect
6965
@@ -85,4 +81,4 @@ jobs:
8581
run: ./vendor/bin/parallel-lint plugins/winter/redirect
8682

8783
- name: Run unit tests
88-
run: php artisan winter:test -p Winter.Redirect
84+
run: php artisan winter:test -p Winter.Redirect

classes/PublishManager.php

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function publish(): int
4343
'ignore_query_parameters',
4444
'ignore_case',
4545
'ignore_trailing_slash',
46+
'forward_query_parameters',
4647
];
4748

4849
/** @var Collection $redirects */

classes/RedirectManager.php

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Winter\Redirect\Classes\Exceptions;
2626
use Winter\Redirect\Classes\Util\Str;
2727
use Winter\Redirect\Models;
28+
use Winter\Storm\Router\UrlGenerator;
2829

2930
final class RedirectManager implements RedirectManagerInterface
3031
{
@@ -237,6 +238,12 @@ public function getLocation(RedirectRule $rule): ?string
237238
$toUrl = str_replace(['https://', 'http://'], $rule->getToScheme() . '://', $toUrl);
238239
}
239240

241+
if ($rule->isForwardQueryParameters() && $params = \request()->query()) {
242+
$toUrl = UrlGenerator::buildUrl($toUrl, [
243+
'query' => $params
244+
]);
245+
}
246+
240247
return $toUrl;
241248
}
242249

classes/RedirectRule.php

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class RedirectRule
2929
private bool $ignoreQueryParameters;
3030
private bool $ignoreCase;
3131
private bool $ignoreTrailingSlash;
32+
private bool $forwardQueryParameters;
3233

3334
public function __construct(array $attributes)
3435
{
@@ -94,6 +95,7 @@ public function __construct(array $attributes)
9495
$this->ignoreQueryParameters = (bool) ($attributes['ignore_query_parameters'] ?? false);
9596
$this->ignoreCase = (bool) ($attributes['ignore_case'] ?? false);
9697
$this->ignoreTrailingSlash = (bool) ($attributes['ignore_trailing_slash'] ?? false);
98+
$this->forwardQueryParameters = (bool) ($attributes['forward_query_parameters'] ?? false);
9799
}
98100

99101
public static function createWithModel(Redirect $model): RedirectRule
@@ -233,4 +235,9 @@ public function isIgnoreTrailingSlash(): bool
233235
{
234236
return $this->ignoreTrailingSlash;
235237
}
238+
239+
public function isForwardQueryParameters(): bool
240+
{
241+
return $this->forwardQueryParameters;
242+
}
236243
}

lang/en/lang.php

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
'ignore_case_comment' => 'The redirect engine will do case-insensitive matching.',
113113
'ignore_trailing_slash' => 'Ignore trailing slash',
114114
'ignore_trailing_slash_comment' => 'The redirect engine will ignore trailing slashes.',
115+
'forward_query_parameters' => 'Forward query parameters',
116+
'forward_query_parameters_comment' => 'The redirect engine will forward query parameters.',
115117
'last_used_at' => 'Last hit',
116118
'updated_at' => 'Updated at',
117119
'invalid_regex' => 'Invalid regular expression.',

models/Redirect.php

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ final class Redirect extends Model
148148
'ignore_query_parameters' => 'boolean',
149149
'ignore_case' => 'boolean',
150150
'ignore_trailing_slash' => 'boolean',
151+
'forward_query_parameters' => 'boolean',
151152
'is_enabled' => 'boolean',
152153
'test_lab' => 'boolean',
153154
'system' => 'boolean',

models/redirect/fields.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ tabs:
114114
span: left
115115
default: true
116116
comment: winter.redirect::lang.redirect.ignore_trailing_slash_comment
117+
forward_query_parameters:
118+
label: winter.redirect::lang.redirect.forward_query_parameters
119+
type: checkbox
120+
span: left
121+
default: true
122+
comment: winter.redirect::lang.redirect.forward_query_parameters_comment
117123

118124
#
119125
# Requirements

phpunit.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
2+
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
bootstrap="../../../tests/bootstrap.php"
54
backupGlobals="false"
65
backupStaticAttributes="false"
76
colors="true"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Winter\Redirect\Updates;
6+
7+
use Winter\Storm\Database\Schema\Blueprint;
8+
use Winter\Storm\Database\Updates\Migration;
9+
use Winter\Storm\Support\Facades\Schema;
10+
11+
class AddQueryParametersForward extends Migration
12+
{
13+
public function up(): void
14+
{
15+
Schema::table('winter_redirect_redirects', function (Blueprint $table) {
16+
$table->boolean('forward_query_parameters')
17+
->default(false)
18+
->after('ignore_trailing_slash');
19+
});
20+
}
21+
22+
public function down(): void
23+
{
24+
Schema::table('winter_redirect_redirects', function (Blueprint $table) {
25+
$table->dropColumn('forward_query_parameters');
26+
});
27+
}
28+
}

updates/version.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@
7575
- "Renamed to Winter.Redirect, forked for use as a Winter CMS plugin."
7676
- 20220415_0013_rename_to_winter_redirect.php
7777
"4.0.2": "Maintained compatibility with Winter 1.1, fixed issue with trailing data on imports"
78+
"4.0.3":
79+
- "Added support for forwarding query string params"
80+
- 20220728_0014_add_forward_query_parameters.php

0 commit comments

Comments
 (0)