Search code examples
typo3typo3-6.1.xtypo3-flow

Typo3 Dependency Injection Container


Which class do I use to add new dependency container configurations?

1. \TYPO3\CMS\Extbase\Object\Container
2. \TYPO3\CMS\Extbase\Object\ObjectManager

Container has 2 appropriate methods:

1.1 getInstance ($className, $givenConstructorArguments=array())
1.2 registerImplementation ($className, $alternativeClassName)

ObjectManager has 2 appropriate methods:

1. get ($objectName)
2. create ($objectName)

Also:

Where do I register the dependencies?

In the global config file /Typo3/LocalConfiguration.php?

What I want is to preconfigure the DI Container singleton with the locations of all public classes. My extensions then ask the Container for an object by its name and they get the object back.

Edit 1:

I read the question here How do I include or autoload external libraries in a TYPO3 Extbase Extension? + Dependecy Injection?
However what I've noticed is that his classes register themselves in the DI Container (using Objectmanager.create)

Additionally I read here http://forge.typo3.org/projects/typo3v4-mvc/wiki/Dependency_Injection_%28DI%29 However I still cannot understand how to use the DI Container as a standalone service locator.

Edit 2:

Do I perhaps add my dependencies to /ext/sysext/version/ext_autoload.php ? or /typo3conf/extTables.php


Solution

  • You can include or autoload external libraries in a TYPO3 Extbase Extension + Dependecy Injection

    You can include external library within “ext_autoload.php”. You have to create ext_autoload files inside extension.

    $extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('rent_system');
    return array(
    'rent_system_TCPDF' => $extensionPath.'Resources/Private/PHP/tcpdf/tcpdf.php',  
    );
    

    controller :

    /**
       * @var Tx_Extbase_Object_ObjectManagerInterface
       */
         protected $objectManager;
    
        /**
         * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
         */
        public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
             $this->objectManager = $objectManager;
        }
       $pdf = $this->objectManager->create('rent_system_TCPDF');