mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Uri::referrer() now accepts third parameter, if set to true, it returns route without base or language code
This commit is contained in:
parent
31b0510bbd
commit
5593327dbc
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user