PHP API: Accessing Site Configuration¶
The PHP API for Sites comes in two parts:
- Accessing the current, resolved site object
- Finding a site object / configuration via a page or identifier
The first case is relevant when we want to access site configuration in the current request, for example if we want to know which language is currently rendered.
The second case is about accessing site configuration options independent of the current request but based on a page id or a site identifier.
Let’s look at both cases in detail:
Accessing the Current Site Object¶
When rendering the frontend or backend TYPO3 builds a HTTP request object
PSR-15 middleware stack and enriches that with information.
Part of that information are the
SiteLanguage objects. Both
objects are available as attributes on the current request object.
Depending on the context, there are two main ways to access them:
- via the
PSR-7HTTP ServerRequest object directly - for example in a PSR-15 middleware or the admin panel
$GLOBALS['TYPO3_REQUEST']- everywhere you don’t have a ServerRequest object
The first method is preferred if possible as
deprecated in 9.2 and will be removed in future versions.
// current site $site = $request->getAttribute('site'); // current site language $siteLanguage = $request->getAttribute('language');
PSR-7 Request and the extbase request are different things. You cannot
access the site configuration via the extbase request. When in extbase context
use the global access - a better way will be introduced in future versions.
Finding a Site Object¶
When you need to access site configuration for a specific page ID or by identifier,
you can use the
SiteFinder offers the following methods for finding a site:
getSiteByIdentifier(): returns site object for the specified identifier (“folder name”)
getSiteByRootPageId(): returns site object for a specific root page (pid = 0 or is_siteroot set)
getSiteByPageId(): returns site object for a page (walks the root line to find the root page and returns the site configuration)
getAllSites(): returns all configured site objects
All methods for finding a specific site throw an exception if no site was found.
The Site Object¶
Now we know how to find a site object, but what can it do?
First of all, it gives us access to the site configuration options via
getConfiguration(): returns the complete configuration
getAttribute(): returns a specific configuration attribute (root level configuration only)
It additionally provides methods for accessing related objects (languages / errorHandling):
getErrorHandler(): returns a
PageErrorHandleraccording to the site configuration
getAvailableLanguages(): returns languages available to a user (including access checks)
getLanguageById(): returns a site language object for a language id
Take a look at the class to find out more:
The SiteLanguage Object¶
SiteLanguage object is basically a simple model that represents the configuration options of
the site regarding language as an object and provides getters for those properties.