Skip to content

Commit aa3d392

Browse files
feat(base): add "redirect" config (POC)
--- POC: Tests will be added once this proposal is accepted --- The "redirects" config allows configuring redirects from paths to routes. Example config: "redirects" => { "^/path/regexp" => "an.apps.controller.route.locator" } Use case: * An administrator can configure another app as target for certain paths. Differentiation from Apache redirects: * this allows configuring redirects in one place and avoids spreading the configuration in multiple places. * the target route locator is more expressive/easier to trace than a path Signed-off-by: Thomas Lehmann <t.lehmann@strato.de>
1 parent f72f110 commit aa3d392

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

config/config.sample.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,4 +2438,23 @@
24382438
* Defaults to ``true``
24392439
*/
24402440
'enable_non-accessible_features' => true,
2441+
2442+
/**
2443+
* Request path without /index.php/ maps to a controller path in the form
2444+
* <app name>.<controller name>.<handler>.
2445+
*
2446+
* - For a FooController.php the controller name is "foo" (lowercase)
2447+
* - A handler would be a method in FooController that was annotated with
2448+
* - either #[FrontpageRoute] attribute
2449+
* - or configured in routes.php
2450+
*
2451+
* Defaults to ``[]`` (no redirects)
2452+
*/
2453+
'redirects' => [
2454+
/**
2455+
* Example:
2456+
* '^\/settings' => 'acmesettings.page.index'
2457+
*/
2458+
],
2459+
24412460
];

lib/base.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Tobia De Koninck <tobia@ledfan.be>
5151
* @author Vincent Petry <vincent@nextcloud.com>
5252
* @author Volkan Gezer <volkangezer@gmail.com>
53+
* @author Thomas Lehmann <t.lehmann@strato.de>
5354
*
5455
* @license AGPL-3.0
5556
*
@@ -1047,7 +1048,27 @@ public static function handleRequest(): void {
10471048
OC_App::loadApps(['filesystem', 'logging']);
10481049
OC_App::loadApps();
10491050
}
1050-
Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo());
1051+
$requestPath = $request->getRawPathInfo();
1052+
$redirects = $systemConfig->getValue('redirects', []);
1053+
1054+
if ($redirects) {
1055+
foreach ($redirects as $fromPattern => $toLocator) {
1056+
if (!preg_match('/' . $fromPattern . '/', $requestPath)) {
1057+
continue;
1058+
}
1059+
1060+
try {
1061+
$targetLocation = Server::get(IURLGenerator::class)->linkToRouteAbsolute($toLocator);
1062+
header('Location: ' . $targetLocation);
1063+
return;
1064+
} catch (\Exception) {
1065+
// In case of container exceptions or
1066+
// route not found exceptions we proceed as usual.
1067+
}
1068+
}
1069+
}
1070+
1071+
Server::get(\OC\Route\Router::class)->match($requestPath);
10511072
return;
10521073
} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
10531074
//header('HTTP/1.0 404 Not Found');

0 commit comments

Comments
 (0)