abstract class AbstractController (View source)

Base controller for framework-managed HTTP controllers.

The controller exposes the current PSR-7 request, request data helpers, response builders, and commonly used framework services. Its runtime controller context is initialized by the controller resolver before the action method is invoked, so the class is not intended for standalone use outside the framework dispatch process.

Methods

void
setControllerContext(ServerRequestInterface $request, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory, ContainerInterface $container)

Initializes the controller runtime context before an action is executed.

void
ServerRequestInterface

Returns the current PSR-7 server request for the dispatched action.

ServerRequestInterface
mixed
input(string $key, mixed $default = null)

Returns a combined request input value.

mixed
mixed
query(string $key, mixed $default = null)

Returns a query string parameter.

mixed
array

Returns all query string parameters.

array
mixed
post(string $key, mixed $default = null)

Returns a parsed body value.

mixed
array

Returns all parsed body values.

array
string|null
header(string $name, string|null $default = null)

Returns a request header value.

string|null
array

Returns all request headers grouped by header name.

array
mixed
cookie(string $name, mixed $default = null)

Returns a cookie parameter.

mixed
array

Returns all cookie parameters.

array
mixed
server(string $key, mixed $default = null)

Returns a server parameter.

mixed
array

Returns all server parameters.

array
string
body()

Returns the raw request body.

string
mixed
jsonInput(string $key, mixed $default = null)

Returns a value from the decoded JSON request payload.

mixed
array

Returns the decoded JSON request payload.

array
bool

Reports whether the request content type is JSON.

bool
bool

Reports whether the request Accept header allows JSON responses.

bool
bool

Reports whether the request should be treated as expecting JSON.

bool
UploadedFileInterface|array|null
file(string $name)

Returns an uploaded file entry by name.

UploadedFileInterface|array|null
array
files()

Returns all uploaded files indexed by input name.

array
bool

Reports whether the request was made through AJAX/XHR semantics.

bool
string|null
ip()

Returns the resolved client IP address when available.

string|null
string|null
userAgent(string|null $default = null)

Returns the User-Agent header value.

string|null
string|null
referer(string|null $default = null)

Returns the Referer header value.

string|null
string
method()

Returns the normalized HTTP method.

string
bool
isMethod(HttpMethod|string $method)

Reports whether the current request uses the given HTTP method.

bool
bool
isGet()

Reports whether the request method is GET.

bool
bool
isPost()

Reports whether the request method is POST.

bool
bool
isPut()

Reports whether the request method is PUT.

bool
bool

Reports whether the request method is PATCH.

bool
bool

Reports whether the request method is DELETE.

bool
bool
isHead()

Reports whether the request method is HEAD.

bool
bool

Reports whether the request method is OPTIONS.

bool
string
inputString(string $key, string $default = '')

Returns an input value converted to string semantics.

string
int
inputInt(string $key, int $default = 0)

Returns an input value converted to integer semantics.

int
float
inputFloat(string $key, float $default = 0.0)

Returns an input value converted to float semantics.

float
bool
inputBool(string $key, bool $default = false)

Returns an input value converted to boolean semantics.

bool
ResponseInterface
text(string $content, int $status = 200)

Creates a plain text response.

ResponseInterface
ResponseInterface
html(string $content, int $status = 200)

Creates an HTML response.

ResponseInterface
ResponseInterface
json(array $payload, int $status = 200)

Creates a JSON response from the provided payload.

ResponseInterface
ResponseInterface
redirect(string $to, int $status = 302)

Creates a redirect response.

ResponseInterface
ResponseInterface
download(string $filePath, string|null $downloadName = null, string $contentType = 'application/octet-stream')

Creates a file download response.

ResponseInterface
ResponseInterface
response(string $content = '', int $status = 200, string $contentType = 'text/html; charset=UTF-8')

Creates a generic response with an explicit content type.

ResponseInterface
ResponseInterface
stream(callable $producer, int $status = 200, string $contentType = 'text/plain; charset=UTF-8', array $headers = [])

Creates a streamed response produced by the given callback.

ResponseInterface
app()

Returns the application context available to the controller.

Returns the breadcrumb component service.

router()

Returns the framework router service.

url()

Returns the framework URL generator.

Returns the form validation service.

upload()

Returns the upload factory service.

Returns the translator service.

Returns the filesystem service.

view()

Returns the view renderer service.

flash()

Returns the flash message bag.

object
controllerService(string $id)

Returns a controller-scoped service resolved from the container.

object
void
setLang(string|null $locale)

Sets the active locale on both translator and validator services.

void
string
trans(string $key, array $replacements = [], string|null $locale = null)

Translates a localized key with optional replacements.

string
array
transGroup(string $group, string|null $locale = null)

Returns the complete translation group for the given locale.

array

Details

setControllerContext

final void setControllerContext(ServerRequestInterface $request, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory, ContainerInterface $container)

Initializes the controller runtime context before an action is executed.

This framework-managed entrypoint stores the current request, response factory, stream factory, and service container, then creates the controller context, response helpers, and service accessors used by derived controllers.

Parameters

ServerRequestInterface $request
ResponseFactoryInterface $responseFactory
StreamFactoryInterface $streamFactory
ContainerInterface $container

Return Value

void

request

protected ServerRequestInterface request()

Returns the current PSR-7 server request for the dispatched action.

Return Value

ServerRequestInterface

input

protected mixed input(string $key, mixed $default = null)

Returns a combined request input value.

Parameters

string $key
mixed $default

Return Value

mixed

query

protected mixed query(string $key, mixed $default = null)

Returns a query string parameter.

Parameters

string $key
mixed $default

Return Value

mixed

queryAll

protected array queryAll()

Returns all query string parameters.

Return Value

array

post

protected mixed post(string $key, mixed $default = null)

Returns a parsed body value.

Parameters

string $key
mixed $default

Return Value

mixed

postAll

protected array postAll()

Returns all parsed body values.

Return Value

array

header

protected string|null header(string $name, string|null $default = null)

Returns a request header value.

Parameters

string $name
string|null $default

Return Value

string|null

headers

protected array headers()

Returns all request headers grouped by header name.

Return Value

array
protected mixed cookie(string $name, mixed $default = null)

Returns a cookie parameter.

Parameters

string $name
mixed $default

Return Value

mixed

cookies

protected array cookies()

Returns all cookie parameters.

Return Value

array

server

protected mixed server(string $key, mixed $default = null)

Returns a server parameter.

Parameters

string $key
mixed $default

Return Value

mixed

serverAll

protected array serverAll()

Returns all server parameters.

Return Value

array

body

protected string body()

Returns the raw request body.

Return Value

string

jsonInput

protected mixed jsonInput(string $key, mixed $default = null)

Returns a value from the decoded JSON request payload.

Parameters

string $key
mixed $default

Return Value

mixed

jsonPayload

protected array jsonPayload()

Returns the decoded JSON request payload.

Return Value

array

isJsonRequest

protected bool isJsonRequest()

Reports whether the request content type is JSON.

Return Value

bool

acceptsJson

protected bool acceptsJson()

Reports whether the request Accept header allows JSON responses.

Return Value

bool

expectsJson

protected bool expectsJson()

Reports whether the request should be treated as expecting JSON.

Return Value

bool

file

protected UploadedFileInterface|array|null file(string $name)

Returns an uploaded file entry by name.

Parameters

string $name

Return Value

UploadedFileInterface|array|null

files

protected array files()

Returns all uploaded files indexed by input name.

Return Value

array

isAjaxRequest

protected bool isAjaxRequest()

Reports whether the request was made through AJAX/XHR semantics.

Return Value

bool

ip

protected string|null ip()

Returns the resolved client IP address when available.

Return Value

string|null

userAgent

protected string|null userAgent(string|null $default = null)

Returns the User-Agent header value.

Parameters

string|null $default

Return Value

string|null

referer

protected string|null referer(string|null $default = null)

Returns the Referer header value.

Parameters

string|null $default

Return Value

string|null

method

protected string method()

Returns the normalized HTTP method.

Return Value

string

isMethod

protected bool isMethod(HttpMethod|string $method)

Reports whether the current request uses the given HTTP method.

Parameters

HttpMethod|string $method

Return Value

bool

isGet

protected bool isGet()

Reports whether the request method is GET.

Return Value

bool

isPost

protected bool isPost()

Reports whether the request method is POST.

Return Value

bool

isPut

protected bool isPut()

Reports whether the request method is PUT.

Return Value

bool

isPatch

protected bool isPatch()

Reports whether the request method is PATCH.

Return Value

bool

isDelete

protected bool isDelete()

Reports whether the request method is DELETE.

Return Value

bool

isHead

protected bool isHead()

Reports whether the request method is HEAD.

Return Value

bool

isOptions

protected bool isOptions()

Reports whether the request method is OPTIONS.

Return Value

bool

inputString

protected string inputString(string $key, string $default = '')

Returns an input value converted to string semantics.

Parameters

string $key
string $default

Return Value

string

inputInt

protected int inputInt(string $key, int $default = 0)

Returns an input value converted to integer semantics.

Parameters

string $key
int $default

Return Value

int

inputFloat

protected float inputFloat(string $key, float $default = 0.0)

Returns an input value converted to float semantics.

Parameters

string $key
float $default

Return Value

float

inputBool

protected bool inputBool(string $key, bool $default = false)

Returns an input value converted to boolean semantics.

Parameters

string $key
bool $default

Return Value

bool

text

protected ResponseInterface text(string $content, int $status = 200)

Creates a plain text response.

Parameters

string $content
int $status

Return Value

ResponseInterface

html

protected ResponseInterface html(string $content, int $status = 200)

Creates an HTML response.

Parameters

string $content
int $status

Return Value

ResponseInterface

json

protected ResponseInterface json(array $payload, int $status = 200)

Creates a JSON response from the provided payload.

Parameters

array $payload
int $status

Return Value

ResponseInterface

redirect

protected ResponseInterface redirect(string $to, int $status = 302)

Creates a redirect response.

Parameters

string $to
int $status

Return Value

ResponseInterface

download

protected ResponseInterface download(string $filePath, string|null $downloadName = null, string $contentType = 'application/octet-stream')

Creates a file download response.

Parameters

string $filePath
string|null $downloadName
string $contentType

Return Value

ResponseInterface

response

protected ResponseInterface response(string $content = '', int $status = 200, string $contentType = 'text/html; charset=UTF-8')

Creates a generic response with an explicit content type.

Parameters

string $content
int $status
string $contentType

Return Value

ResponseInterface

stream

protected ResponseInterface stream(callable $producer, int $status = 200, string $contentType = 'text/plain; charset=UTF-8', array $headers = [])

Creates a streamed response produced by the given callback.

Parameters

callable $producer
int $status
string $contentType
array $headers

Return Value

ResponseInterface

app

Returns the application context available to the controller.

Return Value

ApplicationContext

breadcrumb

protected BreadcrumbComponent breadcrumb()

Returns the breadcrumb component service.

Return Value

BreadcrumbComponent

router

protected Router router()

Returns the framework router service.

Return Value

Router

url

protected UrlGenerator url()

Returns the framework URL generator.

Return Value

UrlGenerator

validator

protected FormValidation validator()

Returns the form validation service.

Return Value

FormValidation

upload

protected UploadFactory upload()

Returns the upload factory service.

Return Value

UploadFactory

translator

protected TranslatorInterface translator()

Returns the translator service.

Return Value

TranslatorInterface

filesystem

protected Filesystem filesystem()

Returns the filesystem service.

Return Value

Filesystem

view

protected View view()

Returns the view renderer service.

Return Value

View

flash

protected FlashBagInterface flash()

Returns the flash message bag.

Return Value

FlashBagInterface

controllerService

protected object controllerService(string $id)

Returns a controller-scoped service resolved from the container.

Parameters

string $id

Return Value

object

setLang

protected void setLang(string|null $locale)

Sets the active locale on both translator and validator services.

Parameters

string|null $locale

Return Value

void

trans

protected string trans(string $key, array $replacements = [], string|null $locale = null)

Translates a localized key with optional replacements.

Parameters

string $key
array $replacements
string|null $locale

Return Value

string

transGroup

protected array transGroup(string $group, string|null $locale = null)

Returns the complete translation group for the given locale.

Parameters

string $group
string|null $locale

Return Value

array