Skip to content

Commit

Permalink
[POC] (C11N) lib/base: add "redirect" config
Browse files Browse the repository at this point in the history
WIP: Tests need to be written

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
  • Loading branch information
tholewebgods committed Jun 13, 2024
1 parent 1e4e848 commit dc535a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions config/config.redirects-sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$CONFIG = [
'redirects' => [
// Request path without /index.php/ maps to a controller path in the form
// <app name>.<controller name>.<handler>.

// - For a FooController.php the controller name is "foo" (lowercase)
// - A handler would be a method in FooController that was annotated with
// - either #[FrontpageRoute] attribute
// - or configured in routes.php
'^\/settings' => 'acmesettings.page.index'
],
];
22 changes: 21 additions & 1 deletion lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,27 @@ public static function handleRequest(): void {
OC_App::loadApps(['filesystem', 'logging']);
OC_App::loadApps();
}
Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo());
$requestPath = $request->getRawPathInfo();
$redirects = $systemConfig->getValue('redirects', []);

if ($redirects) {
foreach ($redirects as $fromPattern => $toLocator) {
if (!preg_match('/' . $fromPattern . '/', $requestPath)) {
continue;
}

try {
$targetLocation = Server::get(IURLGenerator::class)->linkToRouteAbsolute($toLocator);
header('Location: ' . $targetLocation);
return;
} catch (\Exception) {
// In case of container exceptions or
// route not found exceptions we proceed as usual.
}
}
}

Server::get(\OC\Route\Router::class)->match($requestPath);
return;
} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
//header('HTTP/1.0 404 Not Found');
Expand Down

0 comments on commit dc535a0

Please sign in to comment.