3

假设我有一个带有 URI 的页面:

 http://mydomain.loc/index.php?id=123&by=name&dir=desc

并且需要添加/更新/删除一些其他参数(让我们将其命名为offset),所以毕竟它将是:

 http://mydomain.loc/index.php?id=123&by=name&dir=desc&offset=321

不幸的是,当用uriBuilder这样的方式建立新链接时

$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder->setArguments(array('offset' => 321))->build();

我只得到index.php?id=123&offset=321(没有bydir争论了......)

我怎样才能强制uriBuilder保留这些论点?(手动重写参数GeneralUtility::_GP(*)是不可能的,因为它们在理论上是未知的)

$_GET数组也不好,因为我正在使用RealURL

4

1 回答 1

5

uriBuilder 有一个setAddQueryString方法,它完全符合您的要求。它将当前查询字符串合并到参数中:

$uri = $this->uriBuilder->setArguments(array('offset' => 321))->setAddQueryString(TRUE)->build();

作为参考,这里是从 TYPO3 核心的实际类中复制的方法:

/**
 * If set, the current query parameters will be merged with $this->arguments. Defaults to FALSE.
 *
 * @param boolean $addQueryString
 * @return \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder the current UriBuilder to allow method chaining
 * @api
 * @see TSref/typolink.addQueryString
 */
public function setAddQueryString($addQueryString) {
    $this->addQueryString = (boolean) $addQueryString;
    return $this;
}
于 2014-07-14T20:48:42.777 回答