Skip to content

Commit

Permalink
Add Forward Query String option (#9)
Browse files Browse the repository at this point in the history
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.
jaxwilko authored Aug 9, 2022
1 parent ff97ae8 commit f6cf11e
Showing 10 changed files with 58 additions and 8 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -16,10 +16,6 @@ jobs:
phpVersion: ['8.0', '8.1']
winterRelease: ['develop']
winterReleaseDir: ['develop']
include:
- phpVersion: '7.4'
winterRelease: 'v1.1.9'
winterReleaseDir: '1.1.9'
fail-fast: false
env:
phpExtensions: mbstring, intl, gd, xml, sqlite
@@ -63,7 +59,7 @@ jobs:
mv winter-${{ matrix.winterReleaseDir }}/* ./
rmdir winter-${{ matrix.winterReleaseDir }}
shopt -u dotglob
cp config/cms.php config/testing/cms.php
[ ! -f config/testing/cms.php ] && cp config/cms.php config/testing/cms.php
mkdir -p plugins/winter
mv redirect-plugin plugins/winter/redirect
@@ -85,4 +81,4 @@ jobs:
run: ./vendor/bin/parallel-lint plugins/winter/redirect

- name: Run unit tests
run: php artisan winter:test -p Winter.Redirect
run: php artisan winter:test -p Winter.Redirect
1 change: 1 addition & 0 deletions classes/PublishManager.php
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ public function publish(): int
'ignore_query_parameters',
'ignore_case',
'ignore_trailing_slash',
'forward_query_parameters',
];

/** @var Collection $redirects */
7 changes: 7 additions & 0 deletions classes/RedirectManager.php
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
use Winter\Redirect\Classes\Exceptions;
use Winter\Redirect\Classes\Util\Str;
use Winter\Redirect\Models;
use Winter\Storm\Router\UrlGenerator;

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

if ($rule->isForwardQueryParameters() && $params = \request()->query()) {
$toUrl = UrlGenerator::buildUrl($toUrl, [
'query' => $params
]);
}

return $toUrl;
}

7 changes: 7 additions & 0 deletions classes/RedirectRule.php
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ final class RedirectRule
private bool $ignoreQueryParameters;
private bool $ignoreCase;
private bool $ignoreTrailingSlash;
private bool $forwardQueryParameters;

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

public static function createWithModel(Redirect $model): RedirectRule
@@ -233,4 +235,9 @@ public function isIgnoreTrailingSlash(): bool
{
return $this->ignoreTrailingSlash;
}

public function isForwardQueryParameters(): bool
{
return $this->forwardQueryParameters;
}
}
2 changes: 2 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
@@ -112,6 +112,8 @@
'ignore_case_comment' => 'The redirect engine will do case-insensitive matching.',
'ignore_trailing_slash' => 'Ignore trailing slash',
'ignore_trailing_slash_comment' => 'The redirect engine will ignore trailing slashes.',
'forward_query_parameters' => 'Forward query parameters',
'forward_query_parameters_comment' => 'The redirect engine will forward query parameters.',
'last_used_at' => 'Last hit',
'updated_at' => 'Updated at',
'invalid_regex' => 'Invalid regular expression.',
1 change: 1 addition & 0 deletions models/Redirect.php
Original file line number Diff line number Diff line change
@@ -148,6 +148,7 @@ final class Redirect extends Model
'ignore_query_parameters' => 'boolean',
'ignore_case' => 'boolean',
'ignore_trailing_slash' => 'boolean',
'forward_query_parameters' => 'boolean',
'is_enabled' => 'boolean',
'test_lab' => 'boolean',
'system' => 'boolean',
6 changes: 6 additions & 0 deletions models/redirect/fields.yaml
Original file line number Diff line number Diff line change
@@ -114,6 +114,12 @@ tabs:
span: left
default: true
comment: winter.redirect::lang.redirect.ignore_trailing_slash_comment
forward_query_parameters:
label: winter.redirect::lang.redirect.forward_query_parameters
type: checkbox
span: left
default: true
comment: winter.redirect::lang.redirect.forward_query_parameters_comment

#
# Requirements
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="../../../tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
28 changes: 28 additions & 0 deletions updates/20220728_0014_add_forward_query_parameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Winter\Redirect\Updates;

use Winter\Storm\Database\Schema\Blueprint;
use Winter\Storm\Database\Updates\Migration;
use Winter\Storm\Support\Facades\Schema;

class AddQueryParametersForward extends Migration
{
public function up(): void
{
Schema::table('winter_redirect_redirects', function (Blueprint $table) {
$table->boolean('forward_query_parameters')
->default(false)
->after('ignore_trailing_slash');
});
}

public function down(): void
{
Schema::table('winter_redirect_redirects', function (Blueprint $table) {
$table->dropColumn('forward_query_parameters');
});
}
}
3 changes: 3 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
@@ -75,3 +75,6 @@
- "Renamed to Winter.Redirect, forked for use as a Winter CMS plugin."
- 20220415_0013_rename_to_winter_redirect.php
"4.0.2": "Maintained compatibility with Winter 1.1, fixed issue with trailing data on imports"
"4.0.3":
- "Added support for forwarding query string params"
- 20220728_0014_add_forward_query_parameters.php

0 comments on commit f6cf11e

Please sign in to comment.