Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

CharsetMiddleware
in package
implements MiddlewareInterface

Charset Middleware

Appends ; charset=<charset> to the response Content-Type for text-ish content types (text/html, text/plain, text/css, text/javascript, application/json, application/xml, application/javascript, image/svg+xml, …) when the Content-Type doesn't already declare a charset.

Apache equivalent:

AddDefaultCharset utf-8
AddCharset utf-8 .html .css .js .json

Usage in app.php:

$app->addMiddleware(new \ZealPHP\Middleware\CharsetMiddleware());          // utf-8
$app->addMiddleware(new \ZealPHP\Middleware\CharsetMiddleware('iso-8859-1'));

Table of Contents

Interfaces

MiddlewareInterface

Constants

TEXTISH_PREFIXES  : mixed = ['text/', 'application/json', 'application/xml'...
Content-Type prefixes that get a charset appended. Anything else (image/png, application/octet-stream, application/pdf, …) is left untouched — sending charset=utf-8 on a binary type is a mild protocol bug some clients log.

Properties

$charset  : string

Methods

__construct()  : mixed
process()  : ResponseInterface
isTextish()  : bool

Constants

TEXTISH_PREFIXES

Content-Type prefixes that get a charset appended. Anything else (image/png, application/octet-stream, application/pdf, …) is left untouched — sending charset=utf-8 on a binary type is a mild protocol bug some clients log.

private mixed TEXTISH_PREFIXES = ['text/', 'application/json', 'application/xml', 'application/javascript', 'application/ld+json', 'application/xhtml+xml', 'image/svg+xml']

Properties

Methods

__construct()

public __construct([string $charset = 'utf-8' ]) : mixed
Parameters
$charset : string = 'utf-8'

process()

public process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
$request : ServerRequestInterface
$handler : RequestHandlerInterface
Return values
ResponseInterface

isTextish()

private isTextish(string $ct) : bool
Parameters
$ct : string
Return values
bool
On this page