Concrete TYPO3 Dependency Injection examples
Published: , Updated:
Introduction ¶
Some real world examples on how to use the Symfony Dependency Injection in TYPO3. E.g. how to inject TypoScript settings or database query builder.
Don't expect to many explanations here. These are already available, see links at bottom. Instead, this blog post contains concrete examples as reference and copy & paste.
Inject DB Connection and QueryBuilder ¶
Given the new Dependency Injection, one can inject a concrete QueryBuilder
instance, or Database Connection. As TYPO3 allows configuring multiple different database servers, developers need to fetch a proper instance based on the concrete database table. This involves some unnecessary code, which can be removed thanks to the Dependency Injection.
services:
_defaults:
autowire: true
autoconfigure: true
public: false
DanielSiepmann\Tracking\:
resource: '../Classes/*'
dbconnection.tx_tracking_pageview:
class: 'TYPO3\CMS\Core\Database\Connection'
factory:
- '@TYPO3\CMS\Core\Database\ConnectionPool'
- 'getConnectionForTable'
arguments:
- 'tx_tracking_pageview'
querybuilder.tx_tracking_pageview:
class: 'TYPO3\CMS\Core\Database\Query\QueryBuilder'
factory:
- '@TYPO3\CMS\Core\Database\ConnectionPool'
- 'getQueryBuilderForTable'
arguments:
- 'tx_tracking_pageview'
DanielSiepmann\Tracking\Domain\Repository\Pageview:
public: true
arguments:
- '@dbconnection.tx_tracking_pageview'
The corresponding PHP could look like:
namespace DanielSiepmann\Tracking\Domain\Repository;
use TYPO3\CMS\Core\Database\Connection;
class Pageview
{
/**
* @var Connection
*/
private $connection;
public function __construct(
Connection $connection
) {
$this->connection = $connection;
}
}
Inject TypoScript Settings ¶
Back in the old days, you could inject the TypoScript Settings into Extbase classes, see my old blog post. Given the new Dependency Injection, those can be injected into all classes, with no additional code inside the class:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Vendor\ExtName\:
resource: '../Classes/*'
extbaseSettings.ExtName.PluginName:
class: 'array'
factory:
- '@TYPO3\CMS\Extbase\Configuration\ConfigurationManager'
- 'getConfiguration'
arguments:
$configurationType: 'Settings'
$extensionName: 'ExtName'
$pluginName: 'PluginName'
Vendor\ExtName\Domain\PeriodCreation\DataHandler:
arguments:
$settings: '@extbaseSettings.ExtName.PluginName'
The TypoScript settings are injected as plain PHP array into the constructor argument $settings
.
Inject Extension Configuration ¶
TYPO3 offers a new API to retrieve extension configuration. This can be used as a factory to provide options via dependency injection. This might be handy under some circumstances, but there might be reasons to inject the API itself.
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Vendor\ExtName\:
resource: '../Classes/*'
extensionconfiguration.ext_key.title:
class: 'string'
factory:
- '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration'
- 'get'
arguments:
- 'ext_key'
- 'title'
Vendor\ExtName\Namespace\Class:
public: true
arguments:
$config: '@extensionconfiguration.ext_key.title'
This example injects a single property from extension configuration. Note that property class
needs to be set to the appropriate type. The $config
argument of the constructor needs to have the same type hint.
Another example would be to inject the whole configuration array. That would then be of type array
and remove the 2nd argument from factory:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Vendor\ExtName\:
resource: '../Classes/*'
extensionconfiguration.ext_key:
class: 'array'
factory:
- '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration'
- 'get'
arguments:
- 'ext_key'
Vendor\ExtName\Namespace\Class:
public: true
arguments:
$config: '@extensionconfiguration.ext_key'
Acknowledgements ¶
Thanks to Nikita Hovratov for providing feedback regarding 2nd example of Inject Extension Configuration. It was wrong as it contained extensionconfiguration.ext_key.title
instead of extensionconfiguration.ext_key
. It was updated 2021-08-17.
Further reading ¶
That are some concrete examples. For further information, check: