Skip to content

ServerRequest: getAttribute Example

Terry L edited this page Jun 20, 2020 · 4 revisions
Clone this wiki locally

Shieldon\Psr7\ServerRequest

Extends Request.

getAttribute($name, $default)

Retrieve a single derived request attribute.

  • param string name * The attribute name.
  • param mixed default = null Default value to return if the attribute does not exist.
  • return mixed

Example:

This example extends to the previous one.

$ip = $serverRequest->getAttribute('ip_address');
$session = $serverRequest->getAttribute('session');

// paymentStatus does not exist.
$paymentStatus = $serverRequest->getAttribute('paymentStatus', 'failed');

echo $ip
// Outputs: 127.0.0.1

echo $session['user_role'];
// Outputs: admin

echo $paymentStatus;
// Outputs: failed