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

Redirects via config file #16355

Open
wants to merge 13 commits into
base: 5.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parameters:
scanFiles:
- lib/craft/behaviors/CustomFieldBehavior.php
- tests/_support/_generated/AcceptanceTesterActions.php
- tests/_support/_generated/ApiTesterActions.php
- tests/_support/_generated/FunctionalTesterActions.php
- tests/_support/_generated/GqlTesterActions.php
- tests/_support/_generated/UnitTesterActions.php
Expand Down
14 changes: 14 additions & 0 deletions src/enums/MatchType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\enums;

enum MatchType: string
{
case Exact = 'exact';
case Regex = 'regex';
}
21 changes: 21 additions & 0 deletions src/events/RedirectEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\events;

use craft\base\Event;
use craft\web\Redirect;

/**
* Redirect event class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class RedirectEvent extends Event
{
public Redirect $redirect;
}
32 changes: 32 additions & 0 deletions src/web/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\events\ExceptionEvent;
use craft\events\RedirectEvent;
use craft\helpers\App;
use craft\helpers\Json;
use craft\helpers\Template;
Expand Down Expand Up @@ -38,6 +39,11 @@ class ErrorHandler extends \yii\web\ErrorHandler
*/
public const EVENT_BEFORE_HANDLE_EXCEPTION = 'beforeHandleException';

/**
* @event RedirectEvent The event that is triggered before a 404 redirect.
*/
public const EVENT_BEFORE_REDIRECT = 'beforeRedirect';

/**
* @inheritdoc
*/
Expand All @@ -57,6 +63,32 @@ public function handleException($exception): void

// 404?
if ($exception instanceof HttpException && $exception->statusCode === 404) {
$redirects = Craft::$app->getConfig()->getConfigFromFile('redirects');
if ($redirects) {
foreach ($redirects as $from => $redirect) {
$callback = function(Redirect $redirect) {
$this->trigger(
self::EVENT_BEFORE_REDIRECT,
new RedirectEvent(['redirect' => $redirect])
);
};

if ($redirect instanceof Redirect) {
$redirect($callback);
continue;
}

$redirectConfig = is_string($redirect) ? ['to' => $redirect] : $redirect;

if (!isset($redirectConfig['from']) && is_string($from)) {
$redirectConfig['from'] = $from;
}

Craft::createObject(Redirect::class, [$redirectConfig])($callback);
}
}


$request = Craft::$app->getRequest();
if ($request->getIsSiteRequest() && $request->getPathInfo() === 'wp-admin') {
$exception->statusCode = 418;
Expand Down
75 changes: 75 additions & 0 deletions src/web/Redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\web;

use Craft;
use craft\enums\MatchType;
use League\Uri\Http;

class Redirect extends \yii\base\BaseObject
{
public string $to;
public string $from;
public \Closure|string $match;
public MatchType $matchType;
public int $statusCode = 302;
public bool $caseSensitive = true;
private string $delimiter = '`';
private array $matches = [];

public function __construct($config = [])
{
$this->match = $config['match'] ?? Craft::$app->getRequest()->getFullPath();
$matchType = $config['matchType'] ?? MatchType::Exact;
$this->matchType = match (true) {
$matchType instanceof MatchType => $matchType,
default => MatchType::tryFrom($matchType),
};

unset($config['match']);
unset($config['matchType']);

parent::__construct($config);
}

public function __invoke(?callable $callback = null): void
{
if (!$this->findMatch()) {
return;
}

if ($callback) {
$callback($this);
}

Craft::$app->getResponse()->redirect($this->replaceMatches($this->to), $this->statusCode);
Craft::$app->end();
}

private function replaceMatches(string $url): string
{
return Craft::$app->getView()->renderObjectTemplate($url, $this->matches);
}

private function findMatch(): bool
{
$url = Http::new(Craft::$app->getRequest()->getAbsoluteUrl());
$match = is_callable($this->match) ? ($this->match)($url) : $this->match;

if ($this->matchType === MatchType::Regex) {
$regexFlags = $this->caseSensitive ? '' : 'i';
$pattern = "{$this->delimiter}{$this->from}{$this->delimiter}{$regexFlags}";

return preg_match($pattern, $match, $this->matches);
}

return $this->caseSensitive ?
strcmp($this->from, $match) === 0 :
strcasecmp($this->from, $match) === 0;
}
}
30 changes: 30 additions & 0 deletions tests/_craft/config/redirects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

return [
// Exact path match
'redirect/from' => 'redirect/to',

// Path match with named capture group
'redirect/from/(?<year>\d{4})' => [
'to' => 'redirect/to/{year}',
'matchType' => 'regex',
'caseSensitive' => false,
],

// Match path and query string
new \craft\web\Redirect([
'from' => 'bar=(?<bar>[^&]+)',
'to' => '/redirect/to/{bar}',
'match' => fn(\Psr\Http\Message\UriInterface $url) => (string) "{$url->getPath()}?{$url->getQuery()}",
'matchType' => \craft\enums\MatchType::Regex,
]),

// Match full URL
'https://craft-5-project.ddev.site/redirect/from/foo/(.+)' => [
'to' => 'https://redirect.to/{1}',
'match' => fn(\Psr\Http\Message\UriInterface $url) => (string) $url,
'matchType' => \craft\enums\MatchType::Regex,
'statusCode' => 301,
'caseSensitive' => false,
],
];
28 changes: 28 additions & 0 deletions tests/_support/ApiTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);


/**
* Inherited Methods
* @method void wantTo($text)
* @method void wantToTest($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause($vars = [])
*
* @SuppressWarnings(PHPMD)
*/
class ApiTester extends \Codeception\Actor
{
use _generated\ApiTesterActions;

/**
* Define custom actions here
*/
}
5 changes: 3 additions & 2 deletions tests/acceptance.suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
- \Helper\Acceptance
- REST:
url: 'https://craft-5-project.ddev.site/'
depends: PhpBrowser
25 changes: 25 additions & 0 deletions tests/api.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

actor: ApiTester
modules:
enabled:
- \Helper\Acceptance
- REST:
url: 'http://localhost/index.php'
depends:
- PhpBrowser
- \craft\test\Craft
config:
\craft\test\Craft:
configFile: 'tests/_craft/config/test.php'
entryUrl: 'http://localhost/index.php'
projectConfig: {}
migrations: []
plugins: []
cleanup: true
transaction: true
dbSetup: {clean: true, setupCraft: true}
66 changes: 66 additions & 0 deletions tests/api/RedirectCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace acceptance;

use ApiTester;
use Codeception\Example;

class RedirectCest
{
public function _before(ApiTester $I)
{
}

/**
* @dataProvider redirectDataProvider
*/
public function testRedirect(ApiTester $I, Example $example): void
{
$I->stopFollowingRedirects();
$I->sendGet($example['fromPath'], $example['fromParams'] ?? []);
$I->seeResponseCodeIs($example['statusCode']);
if (isset($example['to'])) {
$I->haveHttpHeader('Location', $example['to']);
}
}

/**
* @phpstan-ignore-next-line
*/
private function redirectDataProvider(): array
{
return [
[
'fromPath' => '/redirect/from',
'to' => 'https://craft-5.ddev.site/redirect/to',
'statusCode' => 302,
],
[
'fromPath' => '/redirect/FROM',
'statusCode' => 404,
],
[
'fromPath' => '/redirect/FROM/1234',
'to' => 'https://craft-5.ddev.site/redirect/to/1234',
'statusCode' => 302,
],
[
'fromPath' => '/redirect/from/foo',
'fromParams' => ['bar' => 'baz'],
'to' => 'https://craft-5.ddev.site/redirect/to/baz',
'statusCode' => 302,
],
[
'fromPath' => '/REDIRECT/from/foo/bar',
'fromParams' => ['baz' => 'qux'],
'to' => 'https://redirect.to/bar?baz=qux',
'statusCode' => 301,
],
];
}
}
Loading