Context implements SingletonInterface
Contains the state of a current page request, to be used when reading information of the current request in which configuration/context it is used.
Typically, the current main context is initialized very early within each entry-point application, and is then modified overridden in e.g. PSR-15 middlewares (e.g. authentication, preview settings etc).
For most use-cases, the current main context is fetched via GeneralUtility::makeInstance(Context::class), however, if custom settings for a single use-case is necessary, it is recommended to clone the base context:
$mainContext = GeneralUtility::makeInstance(Context::class); $customContext = clone $mainContext; $customContext->setAspect(GeneralUtility::makeInstance(VisibilityAspect::class, true, true, false))
... which in turn can be injected in the various places where TYPO3 uses contexts.
Classic aspect names to be used are:
- date (DateTimeAspect)
- workspace
- visibility
- frontend.user
- backend.user
- language
- frontend.preview [if EXT:frontend is loaded]
- typoscript
Table of Contents
Interfaces
- SingletonInterface
- "empty" interface for singletons (marker interface pattern)
Properties
- $aspects : array<string|int, AspectInterface>
Methods
- __construct() : mixed
- Sets up the context with pre-defined aspects
- getAspect() : AspectInterface
- Returns an aspect, if it is set
- getPropertyFromAspect() : mixed|null
- Returns a property from the aspect, but only if the property is found.
- hasAspect() : bool
- Checks if an aspect exists in the context
- setAspect() : void
- Sets an aspect, or overrides an existing aspect if an aspect is already set
- unsetAspect() : void
Properties
$aspects
protected
array<string|int, AspectInterface>
$aspects
= []
Methods
__construct()
Sets up the context with pre-defined aspects
public
__construct([array<string|int, mixed> $defaultAspects = [] ]) : mixed
Parameters
- $defaultAspects : array<string|int, mixed> = []
getAspect()
Returns an aspect, if it is set
public
getAspect(string $name) : AspectInterface
Parameters
- $name : string
Tags
Return values
AspectInterfacegetPropertyFromAspect()
Returns a property from the aspect, but only if the property is found.
public
getPropertyFromAspect(string $name, string $property[, mixed $default = null ]) : mixed|null
Parameters
- $name : string
- $property : string
- $default : mixed = null
Tags
Return values
mixed|nullhasAspect()
Checks if an aspect exists in the context
public
hasAspect(string $name) : bool
Parameters
- $name : string
Return values
boolsetAspect()
Sets an aspect, or overrides an existing aspect if an aspect is already set
public
setAspect(string $name, AspectInterface $aspect) : void
Parameters
- $name : string
- $aspect : AspectInterface
unsetAspect()
public
unsetAspect(string $name) : void
Parameters
- $name : string
Using this method is a sign of a technical debt. It is used by RedirectService, but may vanish any time when this is fixed, and thus internal. In general, Context aspects should never have to be unset. When a middleware has to use this method, it is either located at the wrong position in the chain, or has some other dependency issue.