VOOZH about

URL: https://deepwiki.com/hypervel/http/3.2-response-types

⇱ Response Types | hypervel/http | DeepWiki


Loading...
Menu

Response Types

This page documents the various response types supported by the Response class and how to create them. The Hypervel HTTP library provides dedicated methods for creating plain text, JSON, HTML views, file downloads, and no-content responses, each with automatic content-type handling and header management.

For information about streaming responses and HTTP range requests, see Streaming & Range Requests. For API response formatting with JSON resources, see JSON Resources. For the underlying Response class architecture and status codes, see Response Object & Contract.


Response Type Categories

The Response class provides five primary response creation methods, each optimized for a specific content type:


Sources: src/Response.php162-255 src/Contracts/ResponseContract.php14-39


Plain Text and Mixed Content Responses

The make() method is the general-purpose response creation method that intelligently determines the appropriate content type based on the input data.

Method Signature

make(mixed $content = '', int $status = 200, array $headers = []): ResponseInterface

Content Type Detection Logic

The make() method applies the following logic to determine content type:

Input TypeContent-Type HeaderBehavior
Array or Arrayableapplication/jsonEncodes to JSON using Json::encode()
Jsonable objectapplication/jsonCalls object's __toString() method
Any other typetext/plainConverts to string
Pre-set Content-Type headerUses existing headerConverts content to string

Implementation Details


Usage Examples

Plain text response:


Array automatically converted to JSON:


Using custom headers:


Sources: src/Response.php162-184 src/Contracts/ResponseContract.php14-17


JSON Responses

The json() method explicitly creates JSON responses with the application/json content type. It extends the parent Hyperf response's json() method with additional header and status code support.

Method Signature

json($data, int $status = 200, array $headers = []): ResponseInterface

Accepted Data Types

The json() method accepts:

  • Plain arrays
  • Objects implementing Hyperf\Contract\Arrayable
  • Objects implementing Hyperf\Contract\Jsonable

Comparison: make() vs json()

Featuremake()json()
IntentGeneral purpose, auto-detectsExplicitly JSON
Content-TypeAuto-detectedAlways application/json
Non-JSON inputConverts to text/plainAttempts JSON encoding
Use caseFlexible response creationAPI endpoints with guaranteed JSON

Implementation

The implementation delegates to the parent HyperfResponse::json() method and then applies custom headers and status code:

src/Response.php220-228

Sources: src/Response.php220-228 src/Contracts/ResponseContract.php29-34


View/HTML Responses

The view() method renders HTML templates using Hyperf's view system and returns an HTTP response with the rendered content.

Method Signature

view(string $view, array $data = [], int $status = 200, array $headers = []): ResponseInterface

Integration with Hyperf View System


Parameters

  • $view: Template name/path (resolved by view system)
  • $data: Associative array passed to template as variables
  • $status: HTTP status code (default: 200)
  • $headers: Additional headers to apply

Dependencies

The view() method requires:

  • Hyperf\View\RenderInterface to be registered in the DI container
  • A configured view engine (Blade, Twig, Plates, etc.)

Sources: src/Response.php202-213 src/Contracts/ResponseContract.php24-27


File Responses

The file() method serves file content with automatic MIME type detection and proper headers.

Method Signature

file(string $path, array $headers = []): ResponseInterface

File Response Flow


MIME Type Detection

The MIME type resolution follows this priority:

  1. Explicit Content-Type in headers - Used if provided (case-insensitive match)
  2. File extension lookup - Uses MimeTypeExtensionGuesser to determine MIME type
  3. Default fallback - application/octet-stream if type cannot be determined

Error Handling

The method throws FileNotFoundException if the file path:

  • Does not exist on the filesystem
  • Is a directory (not a regular file)

Usage Example


Sources: src/Response.php233-255 src/Contracts/ResponseContract.php37-39


No Content Responses

The noContent() method creates responses with no body content, typically used for successful operations that don't need to return data (e.g., DELETE operations, OPTIONS requests).

Method Signature

noContent(int $status = 204, array $headers = []): ResponseInterface

HTTP Status Codes for No Content

While the default status is 204 (No Content), other valid no-content status codes include:

Status CodeConstantUse Case
204Response::HTTP_NO_CONTENTSuccessful DELETE, PUT with no return
205Response::HTTP_RESET_CONTENTReset form/view state
304Response::HTTP_NOT_MODIFIEDCached resource still valid

Implementation

The implementation is straightforward - it creates a response with the specified status code and headers but no body content:

src/Response.php189-197

Usage Patterns

DELETE operation:


Conditional response (cache validation):


Sources: src/Response.php189-197 src/Contracts/ResponseContract.php19-22


Response Type Selection Guide

Use this decision tree to select the appropriate response method:


Method Selection Summary

MethodPrimary Use CaseContent-TypeStatus Default
make()Flexible responses with auto-detectionAuto-detected200
json()Guaranteed JSON API responsesapplication/json200
view()Server-rendered HTML pagestext/html200
file()Serving static filesAuto-detected from extension200
noContent()Success with no return dataNone204

Sources: src/Response.php162-255 src/Contracts/ResponseContract.php12-77


Common Headers Across Response Types

All response creation methods accept an optional $headers array parameter. Headers are applied consistently:

Header Application Pattern


Frequently Used Headers


Sources: src/Response.php162-255