Skip to content

ServerRequest: getAttributes Example

Terry L edited this page Jun 20, 2020 · 3 revisions

Shieldon\Psr7\ServerRequest

Extends Request.

getAttributes()

Retrieve attributes derived from the request.

  • return array

Example:

$_SESSION['user_name'] = 'terrylin';
$_SESSION['user_role'] = 'admin';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';

$serverRequest = $serverRequest->
    withAttribute('session', $_SESSION)->
    withAttribute('ip_address', $_SERVER['REMOTE_ADDR']);

$attributes = $serverRequest->getAttributes();

echo $attributes['session']['user_name'];
// Outputs: terrylin

echo $attributes['ip_address'];
// Outputs: 127.0.0.1
Clone this wiki locally