Menu

## Uri extends KBase
in package
implements UriInterface

Uri.php : 18

Class for managing Uri (PSR-7)

##### Tags

see
  https://www.kotchasan.com/

### Interfaces, Classes and Traits

UriInterface
  Value object representing a URI.

### Table of Contents

$cfg : object
  Config class instance.
$fragment : string
  Uri fragment after #
$host : string
  Uri host
$path : string
  Uri path
$port : int
  Uri port
$query : string
  Uri query string after ?
$request : Request
  Server request class instance.
$scheme : string
  Uri scheme
$userInfo : string
  Uri user info
__construct() : mixed
  Constructs a new Uri object.
__toString() : string
  Magic function to output the class as a String
createBackUri() : string
  Create a URL for redirecting query strings from one page to another to allow creating URLs that can be sent back to the original page with the `back()` function Remove null items
createFromGlobals() : Uri
  Creates a new Uri object based on the current server environment.
createFromUri() : Uri
  Creates a new Uri object based on the provided URI string.
getAuthority() : string
  Retrieves the authority component of the URI.
getBack() : string
  Convert GET parameters to a query string for returning to the original page after form submission.
getFragment() : string
  Get the fragment (data after #) of the Uri.
getHost() : string
  Get the hostname of the Uri, e.g., domain.tld.
getPath() : string
  Get the path of the Uri, e.g., /kotchasan.
getPort() : null|int
  Get the port number of the Uri.
getQuery() : string
  Get the query string (data after ?) of the Uri.
getScheme() : string
  Get the scheme of the Uri without ://, e.g., http, https.
getUserInfo() : string
  Get the user information of the Uri user[:password].
pagination() : string
  Generate a pagination HTML string.
paramsToQuery() : string
  Convert an array of query parameters to a query string.
parseQueryParams() : array<string|int, mixed>
  Parse the query string and return an array of query parameters.
postBack() : string
  Generate a URL with query string for posting back to the original page, typically used with form submissions.
withFragment() : static
  Set the fragment of the URL.
withHost() : static
  Set the host of the URL.
withoutParams() : static
  Remove specified query parameters from the URL.
withoutQuery() : static
  Remove the specified query parameters from the URL.
withParams() : static
  Set or update query parameters in the URL.
withPath() : static
  Set the path of the URI.
withPort() : static
  Set the port number of the URI.
withQuery() : static
  Set the query string of the URI.
withScheme() : static
  Set the scheme (http or https) of the URI.
withUserInfo() : self
  Return a new instance of the class with the specified user information (username and password).
createBack() : string
  Convert POST data to a query string for redirecting back to the previous page after form submission.
createUriString() : string
  Create a URI string.
filterPath() : string
  Validate the path.
filterPort() : int|null
  Filter and validate the port number for a URI.
filterQueryFragment() : string
  Check and filter the query and fragment components of a URL.
filterScheme() : string
  Check the URL scheme.
isNonStandardPort() : bool
  Check if the port is a non-standard port for the given scheme and host.

### Properties

#### $cfg

KBase.php : 26

Config class instance.

`protected static object $cfg`

#### $fragment

Uri.php : 25

Uri fragment after #

`protected string $fragment = ''`

#### $host

Uri.php : 31

Uri host

`protected string $host = ''`

#### $path

Uri.php : 37

Uri path

`protected string $path = ''`

#### $port

Uri.php : 43

Uri port

`protected int $port`

#### $query

Uri.php : 49

Uri query string after ?

`protected string $query = ''`

#### $request

KBase.php : 33

Server request class instance.

`protected static Request $request`

#### $scheme

Uri.php : 55

Uri scheme

`protected string $scheme = ''`

#### $userInfo

Uri.php : 61

Uri user info

`protected string $userInfo = ''`

### Methods

#### __construct()

Uri.php : 75

Constructs a new Uri object.

`public __construct(string $scheme, string $host[, string $path = '/' ][, string $query = '' ][, int|null $port = null ][, string $user = '' ][, string $pass = '' ][, string $fragment = '' ]) : mixed`

##### Parameters

$scheme : string

  The URI scheme (e.g., "http", "https").

$host : string

  The URI host.

$path : string = '/'

  The URI path (default is "/").

$query : string = ''

  The URI query string (default is an empty string).

$port : int|null = null

  The URI port (optional, default is null).

$user : string = ''

  The username for URI authentication (optional, default is an empty string).

$pass : string = ''

  The password for URI authentication (optional, default is an empty string).

$fragment : string = ''

  The URI fragment (part after the # character, optional, default is an empty string).

##### Return values

mixed —

#### __toString()

Uri.php : 91

Magic function to output the class as a String

`public __toString() : string`

##### Return values

string —

#### createBackUri()

Uri.php : 111

Create a URL for redirecting query strings from one page to another to allow creating URLs that can be sent back to the original page with the `back()` function Remove null items

`public createBackUri(array<string|int, mixed> $query_string) : string`

##### Parameters

$query_string : array<string|int, mixed>

##### Return values

string —

#### createFromGlobals()

Uri.php : 139

Creates a new Uri object based on the current server environment.

`public static createFromGlobals() : Uri`

##### Tags

throws
  InvalidArgumentException

  If the provided URI is invalid.

##### Return values

Uri —

A new Uri object based on the server environment.

#### createFromUri()

Uri.php : 192

Creates a new Uri object based on the provided URI string.

`public static createFromUri(string $uri) : Uri`

##### Parameters

$uri : string

  The URI string to create the Uri object from.

##### Tags

throws
  InvalidArgumentException

  If the provided URI is invalid.

##### Return values

Uri —

A new Uri object based on the provided URI.

#### getAuthority()

Uri.php : 221

Retrieves the authority component of the URI.

`public getAuthority() : string`

The authority component consists of the user information, host, and optional port. If the user information is present, it is followed by "@" symbol. If the port is present, it is preceded by ":" symbol.

##### Return values

string —

The authority component of the URI.

#### getBack()

Uri.php : 238

Convert GET parameters to a query string for returning to the original page after form submission.

`public getBack(string $url[, array<string|int, mixed> $query_string = [] ]) : string`

Generate a URL for returning to the original page using the specified base URL and query string. The query string can be provided as an array in the form of key-value pairs. The method automatically includes the query string from the current request by default.

##### Parameters

$url : string

  The URL to return to, e.g., index.php.

$query_string : array<string|int, mixed> = []

  (optional) The query string to include as an array of key-value pairs. Default is an empty array.

##### Return values

string —

The generated URL with the query string.

#### getFragment()

Uri.php : 248

Get the fragment (data after #) of the Uri.

`public getFragment() : string`

##### Return values

string —

The fragment of the Uri.

#### getHost()

Uri.php : 258

Get the hostname of the Uri, e.g., domain.tld.

`public getHost() : string`

##### Return values

string —

The hostname of the Uri.

#### getPath()

Uri.php : 268

Get the path of the Uri, e.g., /kotchasan.

`public getPath() : string`

##### Return values

string —

The path of the Uri.

#### getPort()

Uri.php : 279

Get the port number of the Uri.

`public getPort() : null|int`

If not specified or default port (80, 443), return null.

##### Return values

null|int —

The port number of the Uri.

#### getQuery()

Uri.php : 289

Get the query string (data after ?) of the Uri.

`public getQuery() : string`

##### Return values

string —

The query string of the Uri.

#### getScheme()

Uri.php : 299

Get the scheme of the Uri without ://, e.g., http, https.

`public getScheme() : string`

##### Return values

string —

The scheme of the Uri.

#### getUserInfo()

Uri.php : 309

Get the user information of the Uri user[:password].

`public getUserInfo() : string`

##### Return values

string —

The user information of the Uri.

#### pagination()

Uri.php : 323

Generate a pagination HTML string.

`public pagination(int $totalpage, int $page[, int $maxlink = 9 ]) : string`

##### Parameters

$totalpage : int

  The total number of pages.

$page : int

  The current page.

$maxlink : int = 9

  The maximum number of links to display.

##### Return values

string —

The pagination HTML string.

#### paramsToQuery()

Uri.php : 353

Convert an array of query parameters to a query string.

`public paramsToQuery(array<string|int, mixed> $params, bool $encode) : string`

##### Parameters

$params : array<string|int, mixed>

  An array of query parameters.

$encode : bool

  Whether to encode the query string using '&' (true) or '&' (false).

##### Return values

string —

The generated query string.

#### parseQueryParams()

Uri.php : 375

Parse the query string and return an array of query parameters.

`public parseQueryParams([string|null $query = null ]) : array<string|int, mixed>`

##### Parameters

$query : string|null = null

  The query string to parse (optional). If not provided, the object's query string will be used.

##### Return values

array<string|int, mixed> —

An array of query parameters.

#### postBack()

Uri.php : 404

Generate a URL with query string for posting back to the original page, typically used with form submissions.

`public postBack(string $url[, array<string|int, mixed> $query_string = [] ][, bool $encode = false ]) : string`

##### Parameters

$url : string

  The URL to which the form should be submitted.

$query_string : array<string|int, mixed> = []

  Additional query string parameters to be included in the URL.

$encode : bool = false

  Determines whether the query string should be encoded using & or & (default is false).

##### Return values

string —

The generated URL with query string.

#### withFragment()

Uri.php : 418

Set the fragment of the URL.

`public withFragment(string $fragment) : static`

##### Parameters

$fragment : string

  The fragment to be set.

##### Tags

throws
  InvalidArgumentException

  If the fragment is not a string.

##### Return values

static —

A new instance of the class with the updated fragment.

#### withHost()

Uri.php : 436

Set the host of the URL.

`public withHost(string $host) : static`

##### Parameters

$host : string

  The host to be set.

##### Return values

static —

A new instance of the class with the updated host.

#### withoutParams()

Uri.php : 471

Remove specified query parameters from the URL.

`public withoutParams(string|array<string|int, mixed> $names[, bool $encode = false ]) : static`

##### Parameters

$names : string|array<string|int, mixed>

  The names of attributes to be removed.

$encode : bool = false

  False (default) to connect query string with "&", true to connect with "&".

##### Return values

static —

A new instance of the class without the specified query parameters.

#### withoutQuery()

Uri.php : 541

Remove the specified query parameters from the URL.

`public withoutQuery(array<string|int, mixed> $query) : static`

##### Parameters

$query : array<string|int, mixed>

  An array of query parameters to be removed (e.g., ['q1' => 'value1', 'q2' => 'value2']).

##### Return values

static —

A new instance of the class without the specified query parameters.

#### withParams()

Uri.php : 451

Set or update query parameters in the URL.

`public withParams(array<string|int, mixed> $params[, bool $encode = false ]) : static`

##### Parameters

$params : array<string|int, mixed>

  An array of query parameters to be set or updated.

$encode : bool = false

  False (default) to connect query string with "&", true to connect with "&".

##### Return values

static —

A new instance of the class with the updated query parameters.

#### withPath()

Uri.php : 492

Set the path of the URI.

`public withPath(string $path) : static`

The path must start with '/' (e.g., '/kotchasan'), or it can be empty if it is the root of the domain.

##### Parameters

$path : string

  The path name.

##### Return values

static —

A new instance of the class with the updated path.

#### withPort()

Uri.php : 508

Set the port number of the URI.

`public withPort(null|int $port) : static`

##### Parameters

$port : null|int

  The port number (1-65535) or null.

##### Tags

throws
  InvalidArgumentException

  If the port number is invalid.

##### Return values

static —

A new instance of the class with the updated port number.

#### withQuery()

Uri.php : 523

Set the query string of the URI.

`public withQuery(string $query) : static`

##### Parameters

$query : string

  The query string.

##### Tags

throws
  InvalidArgumentException

  If the query string is invalid.

##### Return values

static —

A new instance of the class with the updated query string.

#### withScheme()

Uri.php : 563

Set the scheme (http or https) of the URI.

`public withScheme(string $scheme) : static`

##### Parameters

$scheme : string

  The scheme (http, https, or empty string).

##### Tags

throws
  InvalidArgumentException

  If the scheme is not empty, http, or https.

##### Return values

static —

A new instance of the class with the updated scheme.

#### withUserInfo()

Uri.php : 578

Return a new instance of the class with the specified user information (username and password).

`public withUserInfo(string $user[, string $password = null ]) : self`

##### Parameters

$user : string

  The username.

$password : string = null

  (optional) The password.

##### Return values

self —

A new instance of the class with the updated user information.

#### createBack()

Uri.php : 596

Convert POST data to a query string for redirecting back to the previous page after form submission.

`private createBack(string $url, array<string|int, mixed> $source, array<string|int, mixed> $query_string[, bool $encode = false ]) : string`

Returns the URL with the query string.

##### Parameters

$url : string

  The URL to redirect back to, e.g., index.php.

$source : array<string|int, mixed>

  The query string from $_POST or $_GET.

$query_string : array<string|int, mixed>

  Additional query string parameters in the format array('key' => 'value', ...).

$encode : bool = false

  False to concatenate query string with '&', true to use '&'.

##### Return values

string —

The URL with the query string.

#### createUriString()

Uri.php : 634

Create a URI string.

`private static createUriString(string $scheme, string $authority, string $path, string $query, string $fragment) : string`

Example: http://domain.tld/

##### Parameters

$scheme : string
$authority : string
$path : string
$query : string
$fragment : string

##### Return values

string —

The URI string.

#### filterPath()

Uri.php : 665

Validate the path.

`private filterPath(string $path) : string`

##### Parameters

$path : string

##### Return values

string —

The filtered path.

#### filterPort()

Uri.php : 682

Filter and validate the port number for a URI.

`private filterPort(string $scheme, string $host, int|null $port) : int|null`

##### Parameters

$scheme : string

  The URI scheme (e.g., http, https).

$host : string

  The URI host.

$port : int|null

  The port number to be filtered and validated.

##### Tags

throws
  InvalidArgumentException

  If the port number is invalid (not within the range of 1 to 65535).

##### Return values

int|null —

The filtered port number, or null if the default port for the given scheme and host is used.

#### filterQueryFragment()

Uri.php : 700

Check and filter the query and fragment components of a URL.

`private filterQueryFragment(string $str) : string`

##### Parameters

$str : string

  The string to filter.

##### Return values

string —

The filtered string.

#### filterScheme()

Uri.php : 715

Check the URL scheme.

`private filterScheme(string $scheme) : string`

##### Parameters

$scheme : string

  The scheme to check.

##### Tags

throws
  InvalidArgumentException

  If the scheme is not empty, "http", or "https".

##### Return values

string —

The filtered scheme.

#### isNonStandardPort()

Uri.php : 739

Check if the port is a non-standard port for the given scheme and host.

`private isNonStandardPort(string $scheme, string $host, int|null $port) : bool`

This method determines if the specified scheme and host combination has a non-standard port. It returns true if either the scheme is not 'http' or 'https', or the port is not 80 or 443. Otherwise, it returns false.

##### Parameters

$scheme : string

  The scheme (e.g., http, https).

$host : string

  The host.

$port : int|null

  The port number.

##### Return values

bool —

True if the port is non-standard, false otherwise.

## Search results