ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Slim\Http\Response Class Reference

Response. More...

+ Inheritance diagram for Slim\Http\Response:
+ Collaboration diagram for Slim\Http\Response:

Public Member Functions

 __construct ( $status=StatusCode::HTTP_OK, HeadersInterface $headers=null, StreamInterface $body=null)
 Create new HTTP response. More...
 
 __clone ()
 This method is applied to the cloned object after PHP performs an initial shallow-copy. More...
 
 getStatusCode ()
 Gets the response status code. More...
 
 withStatus ($code, $reasonPhrase='')
 Return an instance with the specified status code and, optionally, reason phrase. More...
 
 getReasonPhrase ()
 Gets the response reason phrase associated with the status code. More...
 
 withHeader ($name, $value)
 Return an instance with the provided value replacing the specified header. More...
 
 write ($data)
 Write data to the response body. More...
 
 withRedirect ($url, $status=null)
 Redirect. More...
 
 withJson ($data, $status=null, $encodingOptions=0)
 Json. More...
 
 isEmpty ()
 Is this response empty? More...
 
 isInformational ()
 Is this response informational? More...
 
 isOk ()
 Is this response OK? More...
 
 isSuccessful ()
 Is this response successful? More...
 
 isRedirect ()
 Is this response a redirect? More...
 
 isRedirection ()
 Is this response a redirection? More...
 
 isForbidden ()
 Is this response forbidden? More...
 
 isNotFound ()
 Is this response not Found? More...
 
 isClientError ()
 Is this response a client error? More...
 
 isServerError ()
 Is this response a server error? More...
 
 __toString ()
 Convert response to string. More...
 
- Public Member Functions inherited from Slim\Http\Message
 __set ($name, $value)
 Disable magic setter to ensure immutability. More...
 
 getProtocolVersion ()
 Retrieves the HTTP protocol version as a string. More...
 
 withProtocolVersion ($version)
 Return an instance with the specified HTTP protocol version. More...
 
 getHeaders ()
 Retrieves all message header values. More...
 
 hasHeader ($name)
 Checks if a header exists by the given case-insensitive name. More...
 
 getHeader ($name)
 Retrieves a message header value by the given case-insensitive name. More...
 
 getHeaderLine ($name)
 Retrieves a comma-separated string of the values for a single header. More...
 
 withHeader ($name, $value)
 Return an instance with the provided value replacing the specified header. More...
 
 withAddedHeader ($name, $value)
 Return an instance with the specified header appended with the given value. More...
 
 withoutHeader ($name)
 Return an instance without the specified header. More...
 
 getBody ()
 Gets the body of the message. More...
 
 withBody (StreamInterface $body)
 Return an instance with the specified message body. More...
 

Data Fields

const EOL = "\r\n"
 

Protected Member Functions

 filterStatus ($status)
 Filter HTTP status code. More...
 

Protected Attributes

 $status = StatusCode::HTTP_OK
 
 $reasonPhrase = ''
 
- Protected Attributes inherited from Slim\Http\Message
 $protocolVersion = '1.1'
 
 $headers
 
 $body
 

Static Protected Attributes

static $messages
 
- Static Protected Attributes inherited from Slim\Http\Message
static $validProtocolVersions
 

Detailed Description

Response.

This class represents an HTTP response. It manages the response status, headers, and body according to the PSR-7 standard.

https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php

Definition at line 27 of file Response.php.

Constructor & Destructor Documentation

◆ __construct()

Slim\Http\Response::__construct (   $status = StatusCode::HTTP_OK,
HeadersInterface  $headers = null,
StreamInterface  $body = null 
)

Create new HTTP response.

Parameters
int$statusThe response status code.
HeadersInterface | null$headersThe response headers.
StreamInterface | null$bodyThe response body.

Definition at line 134 of file Response.php.

References Slim\Http\Message\$body, Slim\Http\Message\$headers, Slim\Http\Response\$status, and Slim\Http\Response\filterStatus().

138  {
139  $this->status = $this->filterStatus($status);
140  $this->headers = $headers ? $headers : new Headers();
141  $this->body = $body ? $body : new Body(fopen('php://temp', 'r+'));
142  }
filterStatus($status)
Filter HTTP status code.
Definition: Response.php:222
+ Here is the call graph for this function:

Member Function Documentation

◆ __clone()

Slim\Http\Response::__clone ( )

This method is applied to the cloned object after PHP performs an initial shallow-copy.

This method completes a deep-copy by creating new objects for the cloned object's internal reference pointers.

Definition at line 150 of file Response.php.

References Slim\Http\Message\$headers.

151  {
152  $this->headers = clone $this->headers;
153  }

◆ __toString()

Slim\Http\Response::__toString ( )

Convert response to string.

Note: This method is not part of the PSR-7 standard.

Returns
string

Definition at line 512 of file Response.php.

References $name, Sabre\VObject\$output, $values, Slim\Http\Response\EOL, Slim\Http\Message\getBody(), Slim\Http\Message\getHeaderLine(), Slim\Http\Message\getHeaders(), Slim\Http\Message\getProtocolVersion(), Slim\Http\Response\getReasonPhrase(), and Slim\Http\Response\getStatusCode().

513  {
514  $output = sprintf(
515  'HTTP/%s %s %s',
516  $this->getProtocolVersion(),
517  $this->getStatusCode(),
518  $this->getReasonPhrase()
519  );
521  foreach ($this->getHeaders() as $name => $values) {
522  $output .= sprintf('%s: %s', $name, $this->getHeaderLine($name)) . Response::EOL;
523  }
525  $output .= (string)$this->getBody();
526 
527  return $output;
528  }
getHeaders()
Retrieves all message header values.
Definition: Message.php:142
getBody()
Gets the body of the message.
Definition: Message.php:279
getHeaderLine($name)
Retrieves a comma-separated string of the values for a single header.
Definition: Message.php:198
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition: Message.php:80
getReasonPhrase()
Gets the response reason phrase associated with the status code.
Definition: Response.php:247
$values
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ filterStatus()

Slim\Http\Response::filterStatus (   $status)
protected

Filter HTTP status code.

Parameters
int$statusHTTP status code.
Returns
int
Exceptions

Definition at line 222 of file Response.php.

References Slim\Http\Response\$status, Slim\Http\StatusCode\HTTP_CONTINUE, and Slim\Http\StatusCode\HTTP_NETWORK_CONNECTION_TIMEOUT_ERROR.

Referenced by Slim\Http\Response\__construct(), and Slim\Http\Response\withStatus().

223  {
224  if (!is_integer($status) ||
227  ) {
228  throw new InvalidArgumentException('Invalid HTTP status code');
229  }
230 
231  return $status;
232  }
const HTTP_NETWORK_CONNECTION_TIMEOUT_ERROR
Definition: StatusCode.php:80
+ Here is the caller graph for this function:

◆ getReasonPhrase()

Slim\Http\Response::getReasonPhrase ( )

Gets the response reason phrase associated with the status code.

Because a reason phrase is not a required element in a response status line, the reason phrase value MAY be null. Implementations MAY choose to return the default RFC 7231 recommended reason phrase (or those listed in the IANA HTTP Status Code Registry) for the response's status code.

http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml string Reason phrase; must return an empty string if none present.

Implements Psr\Http\Message\ResponseInterface.

Definition at line 247 of file Response.php.

References $messages, Slim\Http\Response\$reasonPhrase, and Slim\Http\Response\$status.

Referenced by Slim\Http\Response\__toString().

248  {
249  if ($this->reasonPhrase) {
250  return $this->reasonPhrase;
251  }
252  if (isset(static::$messages[$this->status])) {
254  }
255  return '';
256  }
$messages
Definition: en.php:5
+ Here is the caller graph for this function:

◆ getStatusCode()

Slim\Http\Response::getStatusCode ( )

Gets the response status code.

The status code is a 3-digit integer result code of the server's attempt to understand and satisfy the request.

Returns
int Status code.

Implements Psr\Http\Message\ResponseInterface.

Definition at line 167 of file Response.php.

References Slim\Http\Response\$status.

Referenced by Slim\Http\Response\__toString(), Slim\Http\Response\isClientError(), Slim\Http\Response\isEmpty(), Slim\Http\Response\isForbidden(), Slim\Http\Response\isInformational(), Slim\Http\Response\isNotFound(), Slim\Http\Response\isOk(), Slim\Http\Response\isRedirect(), Slim\Http\Response\isRedirection(), Slim\Http\Response\isServerError(), Slim\Http\Response\isSuccessful(), and Slim\Http\Response\withRedirect().

168  {
169  return $this->status;
170  }
+ Here is the caller graph for this function:

◆ isClientError()

Slim\Http\Response::isClientError ( )

Is this response a client error?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 487 of file Response.php.

References Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_BAD_REQUEST, and Slim\Http\StatusCode\HTTP_INTERNAL_SERVER_ERROR.

488  {
489  return $this->getStatusCode() >= StatusCode::HTTP_BAD_REQUEST &&
491  }
const HTTP_INTERNAL_SERVER_ERROR
Definition: StatusCode.php:69
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isEmpty()

Slim\Http\Response::isEmpty ( )

Is this response empty?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 376 of file Response.php.

References Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_NO_CONTENT, Slim\Http\StatusCode\HTTP_NOT_MODIFIED, and Slim\Http\StatusCode\HTTP_RESET_CONTENT.

377  {
378  return in_array(
379  $this->getStatusCode(),
381  );
382  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isForbidden()

Slim\Http\Response::isForbidden ( )

Is this response forbidden?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 463 of file Response.php.

References Slim\Http\Response\getStatusCode(), and Slim\Http\StatusCode\HTTP_FORBIDDEN.

464  {
465  return $this->getStatusCode() === StatusCode::HTTP_FORBIDDEN;
466  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isInformational()

Slim\Http\Response::isInformational ( )

Is this response informational?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 391 of file Response.php.

References Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_CONTINUE, and Slim\Http\StatusCode\HTTP_OK.

392  {
394  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isNotFound()

Slim\Http\Response::isNotFound ( )

Is this response not Found?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 475 of file Response.php.

References Slim\Http\Response\getStatusCode(), and Slim\Http\StatusCode\HTTP_NOT_FOUND.

476  {
477  return $this->getStatusCode() === StatusCode::HTTP_NOT_FOUND;
478  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isOk()

Slim\Http\Response::isOk ( )

Is this response OK?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 403 of file Response.php.

References Slim\Http\Response\getStatusCode(), and Slim\Http\StatusCode\HTTP_OK.

404  {
405  return $this->getStatusCode() === StatusCode::HTTP_OK;
406  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isRedirect()

Slim\Http\Response::isRedirect ( )

Is this response a redirect?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 428 of file Response.php.

References Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_FOUND, Slim\Http\StatusCode\HTTP_MOVED_PERMANENTLY, Slim\Http\StatusCode\HTTP_PERMANENT_REDIRECT, Slim\Http\StatusCode\HTTP_SEE_OTHER, and Slim\Http\StatusCode\HTTP_TEMPORARY_REDIRECT.

429  {
430  return in_array(
431  $this->getStatusCode(),
432  [
438  ]
439  );
440  }
const HTTP_MOVED_PERMANENTLY
Definition: StatusCode.php:29
const HTTP_PERMANENT_REDIRECT
Definition: StatusCode.php:36
const HTTP_TEMPORARY_REDIRECT
Definition: StatusCode.php:35
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isRedirection()

Slim\Http\Response::isRedirection ( )

Is this response a redirection?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 449 of file Response.php.

References Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_BAD_REQUEST, and Slim\Http\StatusCode\HTTP_MULTIPLE_CHOICES.

450  {
453  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isServerError()

Slim\Http\Response::isServerError ( )

Is this response a server error?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 500 of file Response.php.

References Slim\Http\Response\getStatusCode(), and Slim\Http\StatusCode\HTTP_INTERNAL_SERVER_ERROR.

501  {
502  return $this->getStatusCode() >= StatusCode::HTTP_INTERNAL_SERVER_ERROR && $this->getStatusCode() < 600;
503  }
const HTTP_INTERNAL_SERVER_ERROR
Definition: StatusCode.php:69
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ isSuccessful()

Slim\Http\Response::isSuccessful ( )

Is this response successful?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 415 of file Response.php.

References Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_MULTIPLE_CHOICES, and Slim\Http\StatusCode\HTTP_OK.

416  {
417  return $this->getStatusCode() >= StatusCode::HTTP_OK &&
419  }
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ withHeader()

Slim\Http\Response::withHeader (   $name,
  $value 
)

Return an instance with the provided value replacing the specified header.

If a Location header is set and the status code is 200, then set the status code to 302 to mimic what PHP does. See https://github.com/slimphp/Slim/issues/1730

Parameters
string$nameCase-insensitive header field name.
string|string[]$value Header value(s).
Returns
static
Exceptions

Implements Psr\Http\Message\MessageInterface.

Definition at line 273 of file Response.php.

References $name, Slim\Http\StatusCode\HTTP_FOUND, and Slim\Http\StatusCode\HTTP_OK.

Referenced by Slim\Http\Response\withRedirect().

274  {
275  $clone = clone $this;
276  $clone->headers->set($name, $value);
277 
278  if ($clone->getStatusCode() === StatusCode::HTTP_OK && strtolower($name) === 'location') {
279  $clone = $clone->withStatus(StatusCode::HTTP_FOUND);
280  }
281 
282  return $clone;
283  }
+ Here is the caller graph for this function:

◆ withJson()

Slim\Http\Response::withJson (   $data,
  $status = null,
  $encodingOptions = 0 
)

Json.

Note: This method is not part of the PSR-7 standard.

This method prepares the response object to return an HTTP Json response to the client.

Parameters
mixed$dataThe data
int$statusThe HTTP status code.
int$encodingOptionsJson encoding options
Exceptions

Definition at line 352 of file Response.php.

References $data, $response, Slim\Http\Response\$status, and Slim\Http\Message\withBody().

353  {
354  $response = $this->withBody(new Body(fopen('php://temp', 'r+')));
355  $response->body->write($json = json_encode($data, $encodingOptions));
356 
357  // Ensure that the json encoding passed successfully
358  if ($json === false) {
359  throw new \RuntimeException(json_last_error_msg(), json_last_error());
360  }
361 
362  $responseWithJson = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
363  if (isset($status)) {
364  return $responseWithJson->withStatus($status);
365  }
366  return $responseWithJson;
367  }
withBody(StreamInterface $body)
Return an instance with the specified message body.
Definition: Message.php:297
$response
$data
Definition: bench.php:6
+ Here is the call graph for this function:

◆ withRedirect()

Slim\Http\Response::withRedirect (   $url,
  $status = null 
)

Redirect.

Note: This method is not part of the PSR-7 standard.

This method prepares the response object to return an HTTP Redirect response to the client.

Parameters
string | UriInterface$urlThe redirect destination.
int | null$statusThe redirect HTTP status code.
Returns
static

Definition at line 323 of file Response.php.

References Slim\Http\Response\$status, $url, Slim\Http\Response\getStatusCode(), Slim\Http\StatusCode\HTTP_FOUND, Slim\Http\StatusCode\HTTP_OK, and Slim\Http\Response\withHeader().

324  {
325  $responseWithRedirect = $this->withHeader('Location', (string)$url);
326 
327  if (is_null($status) && $this->getStatusCode() === StatusCode::HTTP_OK) {
329  }
330 
331  if (!is_null($status)) {
332  return $responseWithRedirect->withStatus($status);
333  }
334 
335  return $responseWithRedirect;
336  }
withHeader($name, $value)
Return an instance with the provided value replacing the specified header.
Definition: Response.php:273
$url
getStatusCode()
Gets the response status code.
Definition: Response.php:167
+ Here is the call graph for this function:

◆ withStatus()

Slim\Http\Response::withStatus (   $code,
  $reasonPhrase = '' 
)

Return an instance with the specified status code and, optionally, reason phrase.

If no reason phrase is specified, implementations MAY choose to default to the RFC 7231 or IANA recommended reason phrase for the response's status code.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated status and reason phrase.

http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml int $code The 3-digit integer result code to set. string $reasonPhrase The reason phrase to use with the provided status code; if none is provided, implementations MAY use the defaults as suggested in the HTTP specification. static For invalid status code arguments.

Implements Psr\Http\Message\ResponseInterface.

Definition at line 192 of file Response.php.

References $code, $messages, Slim\Http\Response\$reasonPhrase, and Slim\Http\Response\filterStatus().

193  {
194  $code = $this->filterStatus($code);
195 
196  if (!is_string($reasonPhrase) && !method_exists($reasonPhrase, '__toString')) {
197  throw new InvalidArgumentException('ReasonPhrase must be a string');
198  }
199 
200  $clone = clone $this;
201  $clone->status = $code;
202  if ($reasonPhrase === '' && isset(static::$messages[$code])) {
204  }
205 
206  if ($reasonPhrase === '') {
207  throw new InvalidArgumentException('ReasonPhrase must be supplied for this code');
208  }
209 
210  $clone->reasonPhrase = $reasonPhrase;
211 
212  return $clone;
213  }
$code
Definition: example_050.php:99
$messages
Definition: en.php:5
filterStatus($status)
Filter HTTP status code.
Definition: Response.php:222
+ Here is the call graph for this function:

◆ write()

Slim\Http\Response::write (   $data)

Write data to the response body.

Note: This method is not part of the PSR-7 standard.

Proxies to the underlying stream and writes the provided data to it.

Parameters
string$data
Returns
$this

Definition at line 300 of file Response.php.

References $data, and Slim\Http\Message\getBody().

301  {
302  $this->getBody()->write($data);
303 
304  return $this;
305  }
getBody()
Gets the body of the message.
Definition: Message.php:279
$data
Definition: bench.php:6
+ Here is the call graph for this function:

Field Documentation

◆ $messages

Slim\Http\Response::$messages
staticprotected
Initial value:
= [

Definition at line 48 of file Response.php.

◆ $reasonPhrase

Slim\Http\Response::$reasonPhrase = ''
protected

◆ $status

◆ EOL

const Slim\Http\Response::EOL = "\r\n"

Definition at line 125 of file Response.php.

Referenced by Slim\Http\Response\__toString().


The documentation for this class was generated from the following file: