Oihana PHP System

getParam.php

Table of Contents

Functions

getParam()  : mixed
Retrieves a parameter from the HTTP request, supporting query string, body, or both.

Functions

getParam()

Retrieves a parameter from the HTTP request, supporting query string, body, or both.

getParam(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $default = [] ][, string $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : mixed

This helper searches for the requested parameter $name in the request according to the specified $strategy:

  • HttpParamStrategy::QUERY → only query string parameters.
  • HttpParamStrategy::BODY → only parsed body parameters.
  • HttpParamStrategy::BOTH → query string first, then body.

Nested keys are supported via dot notation (e.g., 'user.profile.email'). Body parameters are normalized to an associative array using toAssociativeArray().

If the parameter is not found:

  • Returns the corresponding value in $default[$name] if present.
  • Returns null if no default is provided.
  • Throws DI\NotFoundException if $throwable is true.
Parameters
$request : ServerRequestInterface|null

The PSR-7 server request instance.

$name : string

The parameter name or dot-notated path.

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

Optional default values as an associative array.

$strategy : string = HttpParamStrategy::BOTH

One of HttpParamStrategy::QUERY|BODY|BOTH. Default: BOTH.

$throwable : bool = false

Whether to throw a NotFoundException if parameter is missing. Default: false.

Tags
throws
NotFoundException

If $throwable is true and the parameter is not found.

author

Marc Alcaraz (ekameleon)

since
1.0.0
example
// Query parameter only
$request = ...; // ?name=Alice
getParam($request, 'name', [], HttpParamStrategy::QUERY);

// Body parameter only
$request = ...; // ['user' => ['email' => 'a@b.c']]
getParam($request, 'user.email', [], HttpParamStrategy::BODY);

// Both sources, with fallback
getParam($request, 'foo', ['foo' => 'default'], HttpParamStrategy::BOTH);

// Throw exception if missing
getParam($request, 'bar', [], HttpParamStrategy::BOTH, true);
Return values
mixed

The parameter value if found, otherwise the default value or null.


        
On this page

Search results