Skip to content
Snippets Groups Projects
Commit f452fa80 authored by Elmar Ludwig's avatar Elmar Ludwig Committed by David Siegfried
Browse files

fix behaviour of `url_for` with fragments, fixes #985

Closes #985

Merge request studip/studip!630
parent 2b4aea31
No related branches found
No related tags found
No related merge requests found
......@@ -275,6 +275,11 @@ abstract class StudipController extends Trails_Controller
throw new InvalidArgumentException(__METHOD__ . ' cannot be used with absolute URLs');
}
// Extract fragment (if any)
if (strpos($to, '#') !== false) {
list($args[0], $fragment) = explode('#', $to);
}
// Extract parameters (if any)
$params = [];
if (is_array(end($args))) {
......@@ -289,17 +294,9 @@ abstract class StudipController extends Trails_Controller
return $arg;
}, $args);
// Combine arguments to new $to string
$to = implode('/', $args);
//preserve fragment
$to_parts = explode('#', $to);
$to = $to_parts[0];
$fragment = $to_parts[1] ?? '';
$url = parent::url_for($to);
$url = parent::url_for(...$args);
if ($fragment) {
if (isset($fragment)) {
$url .= '#' . $fragment;
}
return URLHelper::getURL($url, $params);
......
......@@ -185,6 +185,10 @@ final class StudipControllerTest extends Codeception\Test\Unit
'2-actions' => ['dispatch.php/foo/bar', 'foo', 'bar'],
'2-actions-and-parameter' => ['dispatch.php/foo/bar?bar=42', 'foo', 'bar', ['bar' => 42]],
'2-actions-and-parameters' => ['dispatch.php/foo/bar?bar=42&baz=23', 'foo', 'bar', ['bar' => 42, 'baz' => 23]],
'fragment' => ['dispatch.php/foo/bar/42/23#jump', 'foo/bar/42/23#jump'],
'fragment-and-parameters' => ['dispatch.php/foo/bar/42/23#jump', 'foo/bar#jump', 42, 23],
'url-encoding-parameters' => ['dispatch.php/foo/bar/%3Fabc/%2F', 'foo/bar', '?abc', '/'],
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment