Skip to content

Core\Uri::with

$uri->with({…}): Core\Uri$uri->with(
    {…},
): Core\Uri

Returns a new Uri with some parts of the address changed. You name the parts in a block, for example with({ scheme: "https", port: null }). The parts are scheme, host, port, path, query and fragment. Each part you do not name stays the same. The Uri you call it on does not change.

null removes a part. This works for port, query and fragment. An empty query is different: query: "" gives an address that ends with ?.

You write each new part already encoded, the way it appears in the address. The user part (userInfo) is always kept, and with cannot change it.

with throws a RuntimeError when the new address is not valid. A port above 65535 is one case. A path without / at the start, next to a host, is another.

The examples below move a link to https, remove parts of an address, and turn an internal address into a public one.

Options

  • schemestringoptionalThe new scheme, without the :.
  • hoststringoptionalThe new host. An IPv6 address is written in brackets, such as [::1].
  • port?intoptionalThe new port, from 0 to 65535. null removes the port.
  • pathstringoptionalThe new path, already encoded. When the address has a host, the path must start with /.
  • query?stringoptionalThe new query, already encoded and without the ?. null removes the query. "" gives an empty query, so the address ends with ?.
  • fragment?stringoptionalThe new fragment, already encoded and without the #. null removes the fragment.

Return value

Returns Core\Uri. A new Uri. The Uri you call it on does not change. An empty port, as in h:/, is removed from the result.

Errors

  • RuntimeErrorThe port is outside 0 to 65535. It is also thrown when a part makes the address invalid, or when a part would move into another part. For example, a path without / next to a host would become part of the host. The message names the part that moved.

Examples

Move a link to https

<?nvs
// Shows how to change the scheme and the port of an address.
var $link = Core\Uri::parse('http://example.com:8080/account?tab=orders');
// The scheme is replaced, and `null` removes the port. The path and the query stay.
// This prints https://example.com/account?tab=orders.
echo $link->with({ scheme: 'https', port: null }), "\n";
// `$link` does not change. This prints the first address again.
echo $link, "\n";
Output
https://example.com/account?tab=orders
http://example.com:8080/account?tab=orders

Remove parts of an address

<?nvs
// Shows how to remove the query and the fragment of an address, and what happens with a bad path.
var $link = Core\Uri::parse('https://example.com/help?topic=pay#step-2');
// `null` removes a part. This prints https://example.com/help.
echo $link->with({ query: null, fragment: null }), "\n";
// An empty text is not the same as `null`. This address ends with `?`.
echo $link->with({ query: '', fragment: null }), "\n";
// Next to a host, a path must start with `/`. This path does not, so `with` throws an error.
try {
echo $link->with({ path: 'contact' }), "\n";
} catch (RuntimeError $e) {
echo 'not a valid address', "\n";
}
Output
https://example.com/help
https://example.com/help?
not a valid address

Turn an internal address into a public one

<?nvs
// Shows how a server turns the address of an internal service into the address its users see.
class PublicLink
{
public static function of(string $internal): string
{
// Only the scheme, the host and the port change. The path and the query are kept.
return Core\Uri::parse($internal)
->with({ scheme: 'https', host: 'files.example.com', port: null })
->toString();
}
}
// This prints https://files.example.com/download/42?name=report.pdf.
echo PublicLink::of('http://10.0.0.7:9000/download/42?name=report.pdf'), "\n";
Output
https://files.example.com/download/42?name=report.pdf