78 lines
2.1 KiB
PHP
78 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Traits;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
trait ReturnUrlTrait
|
|
{
|
|
public function getReturnUrl(Request $request, string $defaultRoute, array $parameters = []): string
|
|
{
|
|
$returnUrl = $request->query->get('r');
|
|
|
|
// An empty "r" reaches us whenever a link forwards a return url it never got
|
|
// itself, it must not be mistaken for a return url pointing at the root.
|
|
if (false === is_string($returnUrl) || '' === $returnUrl) {
|
|
return $this->generateUrl($defaultRoute, $parameters);
|
|
}
|
|
|
|
return rawurldecode($returnUrl);
|
|
}
|
|
|
|
/**
|
|
* Remove specified query parameters from a URL.
|
|
*
|
|
* @param string $url The URL to process
|
|
* @param string[] $parametersKeys Array of query parameter keys to remove
|
|
*
|
|
* @return string The URL without the specified query parameters
|
|
*/
|
|
public function removeQueryParameters(string $url, array $parametersKeys): string
|
|
{
|
|
if ([] === $parametersKeys) {
|
|
return $url;
|
|
}
|
|
|
|
$urlParts = parse_url($url);
|
|
|
|
if (false === isset($urlParts['query'])) {
|
|
return $url;
|
|
}
|
|
|
|
parse_str($urlParts['query'], $queryParams);
|
|
|
|
foreach ($parametersKeys as $key) {
|
|
unset($queryParams[$key]);
|
|
}
|
|
|
|
$urlParts['query'] = http_build_query($queryParams);
|
|
|
|
if ('' === $urlParts['query']) {
|
|
unset($urlParts['query']);
|
|
}
|
|
|
|
// Rebuild URL
|
|
$result = '';
|
|
if (isset($urlParts['scheme'])) {
|
|
$result .= $urlParts['scheme'].'://';
|
|
}
|
|
if (isset($urlParts['host'])) {
|
|
$result .= $urlParts['host'];
|
|
}
|
|
if (isset($urlParts['port'])) {
|
|
$result .= ':'.$urlParts['port'];
|
|
}
|
|
if (isset($urlParts['path'])) {
|
|
$result .= $urlParts['path'];
|
|
}
|
|
if (isset($urlParts['query'])) {
|
|
$result .= '?'.$urlParts['query'];
|
|
}
|
|
if (isset($urlParts['fragment'])) {
|
|
$result .= '#'.$urlParts['fragment'];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|