75 lines
1.9 KiB
PHP
75 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Traits;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
trait ReturnUrlTrait
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $parameters
|
|
*/
|
|
public function getReturnUrl(Request $request, string $defaultRoute, array $parameters = []): string
|
|
{
|
|
$defaultUrl = $this->generateUrl($defaultRoute, $parameters);
|
|
|
|
return rawurldecode($request->query->get('r', $defaultUrl));
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|