Uri::referrer() now accepts third parameter, if set to true, it returns route without base or language code

This commit is contained in:
Matias Griese 2021-09-20 15:39:09 +03:00
parent 31b0510bbd
commit 5593327dbc
2 changed files with 15 additions and 10 deletions

View File

@ -3,6 +3,8 @@
1. [](#new)
* Composer update
2. [](#improved)
* `Uri::referrer()` now accepts third parameter, if set to `true`, it returns route without base or language code
# v1.7.22
## 09/16/2021

View File

@ -588,31 +588,34 @@ class Uri
*
* @param string|null $default
* @param string|null $attributes
* @param bool $withoutBaseRoute Set to true if you want not to include `/base/lang` (useful for redirects).
* @return string
*/
public function referrer($default = null, $attributes = null)
public function referrer($default = null, $attributes = null, bool $withoutBaseRoute = false)
{
$referrer = $_SERVER['HTTP_REFERER'] ?? null;
// Check that referrer came from our site.
$root = $this->rootUrl(true);
if ($referrer) {
// Referrer should always have host set and it should come from the same base address.
if (stripos($referrer, $root) !== 0) {
$referrer = null;
}
if ($withoutBaseRoute) {
/** @var Pages $pages */
$pages = Grav::instance()['pages'];
$base = $pages->baseUrl(null, true);
} else {
$base = $this->rootUrl(true);
}
if (!$referrer) {
// Referrer should always have host set and it should come from the same base address.
if (!is_string($referrer) || !str_starts_with($referrer, $base)) {
$referrer = $default ?: $this->route(true, true);
}
// Relative path from grav root.
$referrer = substr($referrer, strlen($base));
if ($attributes) {
$referrer .= $attributes;
}
// Return relative path.
return substr($referrer, strlen($root));
return $referrer;
}
/**