\Symfony\Component\HttpFoundationRedirectResponse

RedirectResponse represents an HTTP response doing a redirect.

Summary

Methods
Properties
Constants
__construct()
create()
__toString()
__clone()
prepare()
sendHeaders()
sendContent()
send()
setContent()
getContent()
setProtocolVersion()
getProtocolVersion()
setStatusCode()
getStatusCode()
setCharset()
getCharset()
isCacheable()
isFresh()
isValidateable()
setPrivate()
setPublic()
setImmutable()
isImmutable()
mustRevalidate()
getDate()
setDate()
getAge()
expire()
getExpires()
setExpires()
getMaxAge()
setMaxAge()
setSharedMaxAge()
getTtl()
setTtl()
setClientTtl()
getLastModified()
setLastModified()
getEtag()
setEtag()
setCache()
setNotModified()
hasVary()
getVary()
setVary()
isNotModified()
isInvalid()
isInformational()
isSuccessful()
isRedirection()
isClientError()
isServerError()
isOk()
isForbidden()
isNotFound()
isRedirect()
isEmpty()
closeOutputBuffers()
setContentSafe()
getTargetUrl()
setTargetUrl()
$headers
$statusTexts
HTTP_CONTINUE
HTTP_SWITCHING_PROTOCOLS
HTTP_PROCESSING
HTTP_EARLY_HINTS
HTTP_OK
HTTP_CREATED
HTTP_ACCEPTED
HTTP_NON_AUTHORITATIVE_INFORMATION
HTTP_NO_CONTENT
HTTP_RESET_CONTENT
HTTP_PARTIAL_CONTENT
HTTP_MULTI_STATUS
HTTP_ALREADY_REPORTED
HTTP_IM_USED
HTTP_MULTIPLE_CHOICES
HTTP_MOVED_PERMANENTLY
HTTP_FOUND
HTTP_SEE_OTHER
HTTP_NOT_MODIFIED
HTTP_USE_PROXY
HTTP_RESERVED
HTTP_TEMPORARY_REDIRECT
HTTP_PERMANENTLY_REDIRECT
HTTP_BAD_REQUEST
HTTP_UNAUTHORIZED
HTTP_PAYMENT_REQUIRED
HTTP_FORBIDDEN
HTTP_NOT_FOUND
HTTP_METHOD_NOT_ALLOWED
HTTP_NOT_ACCEPTABLE
HTTP_PROXY_AUTHENTICATION_REQUIRED
HTTP_REQUEST_TIMEOUT
HTTP_CONFLICT
HTTP_GONE
HTTP_LENGTH_REQUIRED
HTTP_PRECONDITION_FAILED
HTTP_REQUEST_ENTITY_TOO_LARGE
HTTP_REQUEST_URI_TOO_LONG
HTTP_UNSUPPORTED_MEDIA_TYPE
HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
HTTP_EXPECTATION_FAILED
HTTP_I_AM_A_TEAPOT
HTTP_MISDIRECTED_REQUEST
HTTP_UNPROCESSABLE_ENTITY
HTTP_LOCKED
HTTP_FAILED_DEPENDENCY
HTTP_TOO_EARLY
HTTP_UPGRADE_REQUIRED
HTTP_PRECONDITION_REQUIRED
HTTP_TOO_MANY_REQUESTS
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
HTTP_INTERNAL_SERVER_ERROR
HTTP_NOT_IMPLEMENTED
HTTP_BAD_GATEWAY
HTTP_SERVICE_UNAVAILABLE
HTTP_GATEWAY_TIMEOUT
HTTP_VERSION_NOT_SUPPORTED
HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL
HTTP_INSUFFICIENT_STORAGE
HTTP_LOOP_DETECTED
HTTP_NOT_EXTENDED
HTTP_NETWORK_AUTHENTICATION_REQUIRED
HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES
ensureIEOverSSLCompatibility()
$content
$version
$statusCode
$statusText
$charset
$targetUrl
N/A
No private methods found
No private properties found
N/A

Constants

HTTP_CONTINUE

HTTP_CONTINUE = 100

HTTP_SWITCHING_PROTOCOLS

HTTP_SWITCHING_PROTOCOLS = 101

HTTP_PROCESSING

HTTP_PROCESSING = 102

HTTP_EARLY_HINTS

HTTP_EARLY_HINTS = 103

HTTP_OK

HTTP_OK = 200

HTTP_CREATED

HTTP_CREATED = 201

HTTP_ACCEPTED

HTTP_ACCEPTED = 202

HTTP_NON_AUTHORITATIVE_INFORMATION

HTTP_NON_AUTHORITATIVE_INFORMATION = 203

HTTP_NO_CONTENT

HTTP_NO_CONTENT = 204

HTTP_RESET_CONTENT

HTTP_RESET_CONTENT = 205

HTTP_PARTIAL_CONTENT

HTTP_PARTIAL_CONTENT = 206

HTTP_MULTI_STATUS

HTTP_MULTI_STATUS = 207

HTTP_ALREADY_REPORTED

HTTP_ALREADY_REPORTED = 208

HTTP_IM_USED

HTTP_IM_USED = 226

HTTP_MULTIPLE_CHOICES

HTTP_MULTIPLE_CHOICES = 300

HTTP_MOVED_PERMANENTLY

HTTP_MOVED_PERMANENTLY = 301

HTTP_FOUND

HTTP_FOUND = 302

HTTP_SEE_OTHER

HTTP_SEE_OTHER = 303

HTTP_NOT_MODIFIED

HTTP_NOT_MODIFIED = 304

HTTP_USE_PROXY

HTTP_USE_PROXY = 305

HTTP_RESERVED

HTTP_RESERVED = 306

HTTP_TEMPORARY_REDIRECT

HTTP_TEMPORARY_REDIRECT = 307

HTTP_PERMANENTLY_REDIRECT

HTTP_PERMANENTLY_REDIRECT = 308

HTTP_BAD_REQUEST

HTTP_BAD_REQUEST = 400

HTTP_UNAUTHORIZED

HTTP_UNAUTHORIZED = 401

HTTP_PAYMENT_REQUIRED

HTTP_PAYMENT_REQUIRED = 402

HTTP_FORBIDDEN

HTTP_FORBIDDEN = 403

HTTP_NOT_FOUND

HTTP_NOT_FOUND = 404

HTTP_METHOD_NOT_ALLOWED

HTTP_METHOD_NOT_ALLOWED = 405

HTTP_NOT_ACCEPTABLE

HTTP_NOT_ACCEPTABLE = 406

HTTP_PROXY_AUTHENTICATION_REQUIRED

HTTP_PROXY_AUTHENTICATION_REQUIRED = 407

HTTP_REQUEST_TIMEOUT

HTTP_REQUEST_TIMEOUT = 408

HTTP_CONFLICT

HTTP_CONFLICT = 409

HTTP_GONE

HTTP_GONE = 410

HTTP_LENGTH_REQUIRED

HTTP_LENGTH_REQUIRED = 411

HTTP_PRECONDITION_FAILED

HTTP_PRECONDITION_FAILED = 412

HTTP_REQUEST_ENTITY_TOO_LARGE

HTTP_REQUEST_ENTITY_TOO_LARGE = 413

HTTP_REQUEST_URI_TOO_LONG

HTTP_REQUEST_URI_TOO_LONG = 414

HTTP_UNSUPPORTED_MEDIA_TYPE

HTTP_UNSUPPORTED_MEDIA_TYPE = 415

HTTP_REQUESTED_RANGE_NOT_SATISFIABLE

HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416

HTTP_EXPECTATION_FAILED

HTTP_EXPECTATION_FAILED = 417

HTTP_I_AM_A_TEAPOT

HTTP_I_AM_A_TEAPOT = 418

HTTP_MISDIRECTED_REQUEST

HTTP_MISDIRECTED_REQUEST = 421

HTTP_UNPROCESSABLE_ENTITY

HTTP_UNPROCESSABLE_ENTITY = 422

HTTP_LOCKED

HTTP_LOCKED = 423

HTTP_FAILED_DEPENDENCY

HTTP_FAILED_DEPENDENCY = 424

HTTP_TOO_EARLY

HTTP_TOO_EARLY = 425

HTTP_UPGRADE_REQUIRED

HTTP_UPGRADE_REQUIRED = 426

HTTP_PRECONDITION_REQUIRED

HTTP_PRECONDITION_REQUIRED = 428

HTTP_TOO_MANY_REQUESTS

HTTP_TOO_MANY_REQUESTS = 429

HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE

HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431

HTTP_INTERNAL_SERVER_ERROR

HTTP_INTERNAL_SERVER_ERROR = 500

HTTP_NOT_IMPLEMENTED

HTTP_NOT_IMPLEMENTED = 501

HTTP_BAD_GATEWAY

HTTP_BAD_GATEWAY = 502

HTTP_SERVICE_UNAVAILABLE

HTTP_SERVICE_UNAVAILABLE = 503

HTTP_GATEWAY_TIMEOUT

HTTP_GATEWAY_TIMEOUT = 504

HTTP_VERSION_NOT_SUPPORTED

HTTP_VERSION_NOT_SUPPORTED = 505

HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL

HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506

HTTP_INSUFFICIENT_STORAGE

HTTP_INSUFFICIENT_STORAGE = 507

HTTP_LOOP_DETECTED

HTTP_LOOP_DETECTED = 508

HTTP_NOT_EXTENDED

HTTP_NOT_EXTENDED = 510

HTTP_NETWORK_AUTHENTICATION_REQUIRED

HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511

HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES

HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = ['must_revalidate' => false, 'no_cache' => false, 'no_store' => false, 'no_transform' => false, 'public' => false, 'private' => false, 'proxy_revalidate' => false, 'max_age' => true, 's_maxage' => true, 'immutable' => false, 'last_modified' => true, 'etag' => true]

Properties

$headers

$headers : \Symfony\Component\HttpFoundation\ResponseHeaderBag

Type

ResponseHeaderBag

$statusTexts

$statusTexts : array

Status codes translation table.

The list of codes is complete according to the {@link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml} (last updated 2021-10-01).

Unless otherwise noted, the status code is defined in RFC2616.

Type

array

$content

$content : string

Type

string

$version

$version : string

Type

string

$statusCode

$statusCode : int

Type

int

$statusText

$statusText : string

Type

string

$charset

$charset : string

Type

string

$targetUrl

$targetUrl

Methods

__construct()

__construct(string  $url, int  $status = 302, array  $headers = []) : mixed

Creates a redirect response so that it conforms to the rules defined for a redirect status code.

Parameters

string $url

The URL to redirect to. The URL should be a full URL, with schema etc., but practically every browser redirects on paths only as well

int $status

The status code (302 by default)

array $headers

The headers (Location is always set to the given URL)

Throws

\InvalidArgumentException

Returns

mixed —

create()

create(string  $url = '', int  $status = 302, array  $headers = []) : static

Factory method for chainability.

Parameters

string $url

The URL to redirect to

int $status
array $headers

Returns

static —

__toString()

__toString() : string

Returns the Response as an HTTP string.

The string representation of the Response is the same as the one that will be sent to the client only if the prepare() method has been called before.

Returns

string —

__clone()

__clone() : mixed

Clones the current Response instance.

Returns

mixed —

prepare()

prepare(\Symfony\Component\HttpFoundation\Request  $request) : $this

Prepares the Response before it is sent to the client.

This method tweaks the Response to ensure that it is compliant with RFC 2616. Most of the changes are based on the Request that is "associated" with this Response.

Parameters

\Symfony\Component\HttpFoundation\Request $request

Returns

$this —

sendHeaders()

sendHeaders() : $this

Sends HTTP headers.

Returns

$this —

sendContent()

sendContent() : $this

Sends content for the current web response.

Returns

$this —

send()

send() : $this

Sends HTTP headers and content.

Returns

$this —

setContent()

setContent(?string  $content) : $this

Sets the response content.

Parameters

?string $content

Returns

$this —

getContent()

getContent() : string|false

Gets the current response content.

Returns

string|false —

setProtocolVersion()

setProtocolVersion(string  $version) : $this

Sets the HTTP protocol version (1.0 or 1.1).

Parameters

string $version

Returns

$this —

getProtocolVersion()

getProtocolVersion() : string

Gets the HTTP protocol version.

Returns

string —

setStatusCode()

setStatusCode(int  $code, string  $text = null) : $this

Sets the response status code.

If the status text is null it will be automatically populated for the known status codes and left empty otherwise.

Parameters

int $code
string $text

Throws

\InvalidArgumentException

When the HTTP status code is not valid

Returns

$this —

getStatusCode()

getStatusCode() : int

Retrieves the status code for the current web response.

Returns

int —

setCharset()

setCharset(string  $charset) : $this

Sets the response charset.

Parameters

string $charset

Returns

$this —

getCharset()

getCharset() : ?string

Retrieves the response charset.

Returns

?string —

isCacheable()

isCacheable() : bool

Returns true if the response may safely be kept in a shared (surrogate) cache.

Responses marked "private" with an explicit Cache-Control directive are considered uncacheable.

Responses with neither a freshness lifetime (Expires, max-age) nor cache validator (Last-Modified, ETag) are considered uncacheable because there is no way to tell when or how to remove them from the cache.

Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation, for example "status codes that are defined as cacheable by default [...] can be reused by a cache with heuristic expiration unless otherwise indicated" (https://tools.ietf.org/html/rfc7231#section-6.1)

Returns

bool —

isFresh()

isFresh() : bool

Returns true if the response is "fresh".

Fresh responses may be served from cache without any interaction with the origin. A response is considered fresh when it includes a Cache-Control/max-age indicator or Expires header and the calculated age is less than the freshness lifetime.

Returns

bool —

isValidateable()

isValidateable() : bool

Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.

Returns

bool —

setPrivate()

setPrivate() : $this

Marks the response as "private".

It makes the response ineligible for serving other clients.

Returns

$this —

setPublic()

setPublic() : $this

Marks the response as "public".

It makes the response eligible for serving other clients.

Returns

$this —

setImmutable()

setImmutable(bool  $immutable = true) : $this

Marks the response as "immutable".

Parameters

bool $immutable

Returns

$this —

isImmutable()

isImmutable() : bool

Returns true if the response is marked as "immutable".

Returns

bool —

mustRevalidate()

mustRevalidate() : bool

Returns true if the response must be revalidated by shared caches once it has become stale.

This method indicates that the response must not be served stale by a cache in any circumstance without first revalidating with the origin. When present, the TTL of the response should not be overridden to be greater than the value provided by the origin.

Returns

bool —

getDate()

getDate() : ?\DateTimeInterface

Returns the Date header as a DateTime instance.

Throws

\RuntimeException

When the header is not parseable

Returns

?\DateTimeInterface —

setDate()

setDate(\DateTimeInterface  $date) : $this

Sets the Date header.

Parameters

\DateTimeInterface $date

Returns

$this —

getAge()

getAge() : int

Returns the age of the response in seconds.

Returns

int —

expire()

expire() : $this

Marks the response stale by setting the Age header to be equal to the maximum age of the response.

Returns

$this —

getExpires()

getExpires() : ?\DateTimeInterface

Returns the value of the Expires header as a DateTime instance.

Returns

?\DateTimeInterface —

setExpires()

setExpires(\DateTimeInterface  $date = null) : $this

Sets the Expires HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

\DateTimeInterface $date

Returns

$this —

getMaxAge()

getMaxAge() : ?int

Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh.

First, it checks for a s-maxage directive, then a max-age directive, and then it falls back on an expires header. It returns null when no maximum age can be established.

Returns

?int —

setMaxAge()

setMaxAge(int  $value) : $this

Sets the number of seconds after which the response should no longer be considered fresh.

This methods sets the Cache-Control max-age directive.

Parameters

int $value

Returns

$this —

setSharedMaxAge()

setSharedMaxAge(int  $value) : $this

Sets the number of seconds after which the response should no longer be considered fresh by shared caches.

This methods sets the Cache-Control s-maxage directive.

Parameters

int $value

Returns

$this —

getTtl()

getTtl() : ?int

Returns the response's time-to-live in seconds.

It returns null when no freshness information is present in the response.

When the response's TTL is 0, the response may not be served from cache without first revalidating with the origin.

Returns

?int —

setTtl()

setTtl(int  $seconds) : $this

Sets the response's time-to-live for shared caches in seconds.

This method adjusts the Cache-Control/s-maxage directive.

Parameters

int $seconds

Returns

$this —

setClientTtl()

setClientTtl(int  $seconds) : $this

Sets the response's time-to-live for private/client caches in seconds.

This method adjusts the Cache-Control/max-age directive.

Parameters

int $seconds

Returns

$this —

getLastModified()

getLastModified() : ?\DateTimeInterface

Returns the Last-Modified HTTP header as a DateTime instance.

Throws

\RuntimeException

When the HTTP header is not parseable

Returns

?\DateTimeInterface —

setLastModified()

setLastModified(\DateTimeInterface  $date = null) : $this

Sets the Last-Modified HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

\DateTimeInterface $date

Returns

$this —

getEtag()

getEtag() : ?string

Returns the literal value of the ETag HTTP header.

Returns

?string —

setEtag()

setEtag(string|null  $etag = null, bool  $weak = false) : $this

Sets the ETag value.

Parameters

string|null $etag

The ETag unique identifier or null to remove the header

bool $weak

Whether you want a weak ETag or not

Returns

$this —

setCache()

setCache(array  $options) : $this

Sets the response's cache headers (validation and/or expiration).

Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.

Parameters

array $options

Throws

\InvalidArgumentException

Returns

$this —

setNotModified()

setNotModified() : $this

Modifies the response so that it conforms to the rules defined for a 304 status code.

This sets the status, removes the body, and discards any headers that MUST NOT be included in 304 responses.

Returns

$this —

hasVary()

hasVary() : bool

Returns true if the response includes a Vary header.

Returns

bool —

getVary()

getVary() : array

Returns an array of header names given in the Vary header.

Returns

array —

setVary()

setVary(string|array  $headers, bool  $replace = true) : $this

Sets the Vary header.

Parameters

string|array $headers
bool $replace

Whether to replace the actual value or not (true by default)

Returns

$this —

isNotModified()

isNotModified(\Symfony\Component\HttpFoundation\Request  $request) : bool

Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.

If the Response is not modified, it sets the status code to 304 and removes the actual content by calling the setNotModified() method.

Parameters

\Symfony\Component\HttpFoundation\Request $request

Returns

bool —

isInvalid()

isInvalid() : bool

Is response invalid?

Returns

bool —

isInformational()

isInformational() : bool

Is response informative?

Returns

bool —

isSuccessful()

isSuccessful() : bool

Is response successful?

Returns

bool —

isRedirection()

isRedirection() : bool

Is the response a redirect?

Returns

bool —

isClientError()

isClientError() : bool

Is there a client error?

Returns

bool —

isServerError()

isServerError() : bool

Was there a server side error?

Returns

bool —

isOk()

isOk() : bool

Is the response OK?

Returns

bool —

isForbidden()

isForbidden() : bool

Is the response forbidden?

Returns

bool —

isNotFound()

isNotFound() : bool

Is the response a not found error?

Returns

bool —

isRedirect()

isRedirect(string  $location = null) : bool

Is the response a redirect of some form?

Parameters

string $location

Returns

bool —

isEmpty()

isEmpty() : bool

Is the response empty?

Returns

bool —

closeOutputBuffers()

closeOutputBuffers(int  $targetLevel, bool  $flush) : void

Cleans or flushes output buffers up to target level.

Resulting level can be greater than target level if a non-removable buffer has been encountered.

Parameters

int $targetLevel
bool $flush

setContentSafe()

setContentSafe(bool  $safe = true) : void

Marks a response as safe according to RFC8674.

Parameters

bool $safe

getTargetUrl()

getTargetUrl() : string

Returns the target URL.

Returns

string —

setTargetUrl()

setTargetUrl(string  $url) : $this

Sets the redirect target of this response.

Parameters

string $url

Throws

\InvalidArgumentException

Returns

$this —

ensureIEOverSSLCompatibility()

ensureIEOverSSLCompatibility(\Symfony\Component\HttpFoundation\Request  $request) : void

Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.

Parameters

\Symfony\Component\HttpFoundation\Request $request