Adding $queryData parameter to Pico::getPageUrl() method
This allows developers to easily add custom query data to an page URL without the need to check enabled URL rewriting on their own. Since Twigs `link` filter is just an alias for Pico::getPageUrl(), theme designers can do the same with e.g. `{{ "index"|link("foo=bar&baz=42") }}`. Theme designers, heads up! Don't forget that the result of the `link` filter is never escaped, so the result could contain unescaped ampersands when passing custom query data. You should pass the result to Twigs `escape` filter when using custom query data.
This commit is contained in:
parent
d9788c12e4
commit
0c85d70820
|
@ -7,6 +7,7 @@ Released: -
|
||||||
```
|
```
|
||||||
* [New] This is Picos first stable release! The Pico Community wants to thank
|
* [New] This is Picos first stable release! The Pico Community wants to thank
|
||||||
all contributors and users which made this possible!
|
all contributors and users which made this possible!
|
||||||
|
* [New] Adding `$queryData` parameter to `Pico::getPageUrl()` method
|
||||||
* [Changed] Moving `LICENSE` to `LICENSE.md`
|
* [Changed] Moving `LICENSE` to `LICENSE.md`
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
27
lib/Pico.php
27
lib/Pico.php
|
@ -1234,17 +1234,32 @@ class Pico
|
||||||
/**
|
/**
|
||||||
* Returns the URL to a given page
|
* Returns the URL to a given page
|
||||||
*
|
*
|
||||||
* @param string $page identifier of the page to link to
|
* @param string $page identifier of the page to link to
|
||||||
* @return string URL
|
* @param array|string $queryData either an array containing properties to
|
||||||
|
* create a URL-encoded query string from, or a already encoded string
|
||||||
|
* @return string URL
|
||||||
*/
|
*/
|
||||||
public function getPageUrl($page)
|
public function getPageUrl($page, $queryData = null)
|
||||||
{
|
{
|
||||||
|
if (is_array($queryData)) {
|
||||||
|
$queryData = http_build_query($queryData, '', '&');
|
||||||
|
} elseif (($queryData !== null) && !is_string($queryData)) {
|
||||||
|
throw new InvalidArgumentException(
|
||||||
|
'Argument 2 passed to ' . get_called_class() . '::getPageUrl() must be of the type array or string, '
|
||||||
|
. (is_object($queryData) ? get_class($queryData) : gettype($queryData)) . ' given'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!empty($queryData)) {
|
||||||
|
$page = (!empty($page) || $this->isUrlRewritingEnabled()) ? $page : 'index';
|
||||||
|
$queryData = $this->isUrlRewritingEnabled() ? '?' . $queryData : '&' . $queryData;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($page)) {
|
if (empty($page)) {
|
||||||
return $this->getBaseUrl();
|
return $this->getBaseUrl() . $queryData;
|
||||||
} elseif (!$this->isUrlRewritingEnabled()) {
|
} elseif (!$this->isUrlRewritingEnabled()) {
|
||||||
return $this->getBaseUrl() . '?' . rawurlencode($page);
|
return $this->getBaseUrl() . '?' . rawurlencode($page) . $queryData;
|
||||||
} else {
|
} else {
|
||||||
return $this->getBaseUrl() . implode('/', array_map('rawurlencode', explode('/', $page)));
|
return $this->getBaseUrl() . implode('/', array_map('rawurlencode', explode('/', $page))) . $queryData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue