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:
Daniel Rudolf 2015-12-13 22:01:25 +01:00
parent d9788c12e4
commit 0c85d70820
2 changed files with 22 additions and 6 deletions

View file

@ -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`
``` ```

View file

@ -1235,16 +1235,31 @@ 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
* @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 * @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;
} }
} }