VOOZH about

URL: https://deepwiki.com/hypervel/support/4.1-request-and-response

⇱ Request and Response | hypervel/support | DeepWiki


Loading...
Menu

Request and Response

This document covers the HTTP request and response handling layer in the Hypervel support package. It explains how to access request data, validate input, check authentication status, and generate various types of responses using the Request, Response, and Cookie facades.

For routing and URL generation, see Routing and URL Generation. For HTTP client operations to make outgoing requests, see HTTP Client.

Overview

The HTTP layer provides three primary facades for handling HTTP interactions:

  • Request: Access incoming request data, headers, files, and user authentication
  • Response: Generate HTTP responses including JSON, views, files, and redirects
  • Cookie: Manage HTTP cookies with queueing and encryption support

These facades resolve to contract implementations through the service container, following the facade pattern documented in Facade System.


Sources: src/Facades/Request.php1-150 src/Facades/Response.php1-54 src/Facades/Cookie.php1-28

Request Facade

The Request facade provides access to all aspects of the incoming HTTP request, including input data, headers, files, authentication, and URL information.

Facade Resolution

The Request facade resolves to RequestContract in the container:


The contract is bound to Hypervel\Http\Request, which extends Hyperf's base request implementation.

Sources: src/Facades/Request.php146-149

Input Access Methods

The Request facade provides multiple methods for accessing request input data:

MethodPurposeExample
input()Get a single input valueRequest::input('name', 'default')
all()Get all input dataRequest::all()
only()Get subset of inputsRequest::only(['name', 'email'])
except()Get all except specifiedRequest::except(['password'])
query()Get query string parameterRequest::query('page', 1)
post()Get POST data parameterRequest::post('name')
inputs()Get multiple inputsRequest::inputs(['name', 'email'])

Sources: src/Facades/Request.php85-90 src/Facades/Request.php36-38

Type-Safe Input Retrieval

The Request facade provides type-safe accessors that automatically cast input values:

MethodReturn TypeDescription
string()StringableGet input as Stringable instance
str()StringableAlias for string()
integer()intGet input as integer with default
float()floatGet input as float with default
boolean()boolGet input as boolean
date()`Carbon\Carbonnull`
enum()`objectnull`
collect()CollectionGet input as Collection

Example Usage:


Sources: src/Facades/Request.php12-29

Input Validation Methods

Check for presence and state of input values:

MethodDescription
has()Check if key(s) exist
hasAny()Check if any key exists
filled()Check if key has non-empty value
anyFilled()Check if any key is filled
isNotFilled()Check if key is empty
missing()Check if key is absent
exists()Check if key exists in request
isEmptyString()Check if key is empty string

Example Usage:


Sources: src/Facades/Request.php11-36

Conditional Input Processing

Execute callbacks based on input state:

MethodSignature
whenFilled()whenFilled(string $key, callable $callback, callable $default = null)
whenHas()whenHas(string $key, callable $callback, callable $default = null)

Sources: src/Facades/Request.php48-49

Request Modification

Merge or replace request data:

MethodDescription
merge()Merge array into current input
replace()Replace all input with array
mergeIfMissing()Merge only missing keys

Sources: src/Facades/Request.php33-35

File Uploads

Access uploaded files:


Sources: src/Facades/Request.php10 src/Facades/Request.php104-105

Headers and Server Data

Access HTTP headers and server variables:

MethodDescription
header()Get header value
headers()Get all headers
bearerToken()Extract Bearer token from Authorization header
server()Get server variable
ip()Get client IP address
getClientIp()Get client IP (alias)

Sources: src/Facades/Request.php64-65 src/Facades/Request.php50-51 src/Facades/Request.php91 src/Facades/Request.php102

URL and Path Information

Access request URL components:

MethodDescription
url()Get full URL without query string
fullUrl()Get full URL with query string
fullUrlWithQuery()Add query parameters to full URL
fullUrlWithoutQuery()Remove query parameters from full URL
path()Get request path
decodedPath()Get URL-decoded path
segment()Get URL segment by index
segments()Get all URL segments
root()Get application root URL
getScheme()Get scheme (http/https)
getHost()Get hostname
getPort()Get port number
isSecure()Check if HTTPS

Sources: src/Facades/Request.php52-63 src/Facades/Request.php92-98 src/Facades/Request.php23-27 src/Facades/Request.php39-40

Request Type Detection

Determine request content type preferences:

MethodDescription
isJson()Check if request is JSON
expectsJson()Check if client expects JSON response
wantsJson()Check if client wants JSON
accepts()Check if accepts content type
acceptsJson()Check if accepts JSON
acceptsHtml()Check if accepts HTML
acceptsAnyContentType()Check if accepts any type
prefers()Get preferred content type
isXmlHttpRequest()Check if AJAX request
ajax()Check if AJAX (alias)
pjax()Check if PJAX request
prefetch()Check if prefetch request

Sources: src/Facades/Request.php30 src/Facades/Request.php41-47 src/Facades/Request.php66-73

HTTP Method Information

Access and check HTTP methods:

MethodDescription
method()Get HTTP method
isMethod()Check if matches method
getMethod()Get method (PSR-7)

Sources: src/Facades/Request.php62 src/Facades/Request.php103 src/Facades/Request.php119

Route Information

Access dispatched route data:

MethodDescription
route()Get route parameter or DispatchedRoute
routeIs()Check if route matches pattern(s)
getDispatchedRoute()Get DispatchedRoute object
is()Check if path matches pattern(s)
fullUrlIs()Check if full URL matches pattern(s)

Sources: src/Facades/Request.php56-61 src/Facades/Request.php94

Authentication

Access authenticated user:

MethodDescription
user()Get authenticated user for guard
getUserResolver()Get user resolver closure
setUserResolver()Set user resolver closure

Sources: src/Facades/Request.php77-79

Request Validation

Validate request input inline:


For comprehensive validation documentation, see Validator Facade.

Sources: src/Facades/Request.php76

Signed URLs

Verify signed URL signatures:

MethodDescription
hasValidSignature()Check absolute signature validity
hasValidRelativeSignature()Check relative signature validity
hasValidSignatureWhileIgnoring()Check signature ignoring parameters
hasValidRelativeSignatureWhileIgnoring()Check relative signature ignoring parameters

For URL signing, see URL Generation.

Sources: src/Facades/Request.php80-83

Session Access

Access session data from request:


Sources: src/Facades/Request.php74-75

PSR-7 Compatibility

Access PSR-7 request interface:


The Request facade provides full PSR-7 ServerRequestInterface compatibility through proxy methods.

Sources: src/Facades/Request.php84 src/Facades/Request.php107-135

Response Facade

The Response facade provides methods for generating various types of HTTP responses.

Facade Resolution

The Response facade resolves to ResponseContract:


Sources: src/Facades/Response.php50-53

Basic Responses

MethodDescription
make()Create basic response with content
noContent()Return 204 No Content response

Sources: src/Facades/Response.php10-11

JSON Responses

Return JSON-encoded data:


Sources: src/Facades/Response.php13

View Responses

Render Blade templates:


For view rendering details, see Views and Blade Templating.

Sources: src/Facades/Response.php12

File Responses

Serve files to the client:


Sources: src/Facades/Response.php14 src/Facades/Response.php25

Streaming Responses

Stream data to the client:


Sources: src/Facades/Response.php16-17

Range Headers

Support for HTTP range requests:


Sources: src/Facades/Response.php18-20

Specialized Content Types

MethodDescription
xml()Return XML response
html()Return HTML response
raw()Return raw content

Sources: src/Facades/Response.php21-23

Redirects

Create redirect responses:


For comprehensive redirect handling, see Routing.

Sources: src/Facades/Response.php24

Response Modification

The Response facade supports PSR-7 response methods for modification:

MethodDescription
withHeader()Add/replace header
withAddedHeader()Add header without replacing
withoutHeader()Remove header
withStatus()Set status code
withBody()Set response body
withProtocolVersion()Set HTTP protocol version
withCookie()Add cookie to response

Sources: src/Facades/Response.php26-40

Inspecting Response

Access response properties:

MethodDescription
getStatusCode()Get HTTP status code
getReasonPhrase()Get status reason phrase
getHeaders()Get all headers
getHeader()Get specific header
getHeaderLine()Get header as string
hasHeader()Check if header exists
getBody()Get response body stream
getProtocolVersion()Get HTTP version

Sources: src/Facades/Response.php27-40

Cookie Facade

The Cookie facade manages HTTP cookies with queueing, encryption, and expiration.

Facade Resolution


Sources: src/Facades/Cookie.php24-27

Creating Cookies

MethodDescription
make()Create cookie instance
forever()Create long-lived cookie (5 years)
forget()Create cookie to delete existing cookie

Sources: src/Facades/Cookie.php12 src/Facades/Cookie.php17-18

Cookie Queue

Queue cookies to be attached to the next response:


Sources: src/Facades/Cookie.php13-16

Expire Cookie

Create expiration cookie:


Sources: src/Facades/Cookie.php14

Reading Cookies

Access cookie values from request:


Sources: src/Facades/Cookie.php10-11

Request/Response Flow

The following diagram illustrates how a typical HTTP request flows through the Request and Response facades:


Sources: src/Facades/Request.php1-150 src/Facades/Response.php1-54 src/Facades/Cookie.php1-28

Common Usage Patterns

API Response Pattern


Content Negotiation Pattern


File Download with Range Support


Signed URL Verification Pattern


Cookie-Based Authentication


Sources: src/Facades/Request.php1-150 src/Facades/Response.php1-54 src/Facades/Cookie.php1-28

Method Reference Summary

Request Facade Methods by Category


Response Facade Methods by Type

CategoryMethods
Basicmake(), noContent()
Content Typesjson(), xml(), html(), raw()
Viewsview()
Filesfile(), download()
Streamingstream(), streamDownload()
Redirectsredirect()
ModificationwithHeader(), withStatus(), withCookie(), withBody()
InspectiongetStatusCode(), getHeaders(), getBody()
Range SupportwithRangeHeaders(), withoutRangeHeaders(), shouldAppendRangeHeaders()

Cookie Facade Methods

CategoryMethods
Creationmake(), forever(), forget()
Queue Managementqueue(), unqueue(), getQueuedCookies()
Expirationexpire()
Readinghas(), get()

Sources: src/Facades/Request.php1-150 src/Facades/Response.php1-54 src/Facades/Cookie.php1-28

Refresh this wiki

On this page