Skip to content

ServerRequest: getAttribute Example

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


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


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:

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

echo $paymentStatus;
// Outputs: failed