Core\Request::queryParameters
Core\Request::queryParameters(string $name): array<tainted string>Core\Request::queryParameters( string $name, ): array<tainted string>
Returns every value of a query-string parameter that has square brackets. You give the name without the brackets: for tag[]=red&tag[]=new, the name is tag, and the result is ["red", "new"]. Each value is a tainted string, which is text that came from the client.
Empty brackets give a list. Brackets with a key give that key: filter[color]=red gives ["color" => "red"]. A name without brackets gives a list with its one value. A name that is not in the address gives an empty array, so you can loop over the result without a check for null.
A name with two pairs of brackets, such as filter[color][dark]=navy, throws a ParseError. Read those values with Core\Request::queryParametersAs and a type that describes them. A command-line program has no request, so queryParameters throws a LogicError there.
The examples below filter a product list, read a list and a missing name, and show the error for two pairs of brackets.
Parameters
$namestringThe parameter's name without the brackets. Fortag[]=a, the name istag.
Return value
Returns array<tainted string>. An array<tainted string>. tag[]=a&tag[]=b gives ["a", "b"], and filter[color]=red gives ["color" => "red"]. A name without brackets gives an array with its one value. A name that is not in the query gives an empty array.
Errors
LogicErrorThis program is not handling a request.RuntimeErrorThe query string has a percent escape that decodes to bytes that are not UTF-8.ParseErrorA value under this name has a second pair of brackets, such astag[a][b]=c. Read nested values withqueryParametersAs.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Request::queryParameter, queryParameters and queryParametersAs read the query string. formField, formFields and formFieldsAs read a submitted form. The old names were query, queryArray, queryAs, post, postArray and postAs. |
Examples
Filtering a product list
<?nvs// Shows how a shop filters its product list with the parameters of the address.
array<string> $products = ['red shirt' => 'red', 'blue shirt' => 'blue', 'red hat' => 'red'];
// The address is `/products?filter[color]=red&filter[size]=M`.// `queryParameters` returns the values under "filter" as an array with the keys "color" and "size".array<tainted string> $filter = Core\Request::queryParameters('filter');tainted string $color = $filter['color'] ?? '';
// This prints "red shirt" and "red hat".foreach ($products as string $name => string $productColor) { if ($color == '' || $productColor == $color) { echo $name, "\n"; }}red shirtred hatSeveral values with one name
<?nvs// Shows what `Core\Request::queryParameters` returns for a list, a single value and a missing name.
// The address is `/search?tag[]=red&tag[]=new&color=blue`.// `queryParameters` returns every value of "tag" as a list. This prints "red" and "new".foreach (Core\Request::queryParameters('tag') as tainted string $tag) { echo $tag, "\n";}
// "color" has no brackets. The result is a list with its one value. This prints "colors: 1".echo 'colors: ', Core\Arr::count(Core\Request::queryParameters('color')), "\n";
// "size" is not in the address, so the result is an empty array. This prints "sizes: 0".echo 'sizes: ', Core\Arr::count(Core\Request::queryParameters('size')), "\n";rednewcolors: 1sizes: 0A name with two pairs of brackets
<?nvs// Shows the error `Core\Request::queryParameters` throws for a name with two pairs of brackets.
// In `/search?filter[color][dark]=navy`, "filter" has two pairs of brackets.// `queryParameters` throws a `ParseError` for it. This prints the message of the error.try { array<tainted string> $filter = Core\Request::queryParameters('filter'); echo 'filters: ', Core\Arr::count($filter), "\n";} catch (ParseError $error) { echo $error->message, "\n";}Core\Request::queryParameters(): a value under this name has a second pair of brackets. Read nested values with `queryParametersAs`