[TYPO3-mvc] How to get to typoscipt ?

Franz Koch typo3.RemoveForMessage at elements-net.de
Fri Jan 14 00:30:48 CET 2011


Hey Dawid,

> Franz do you have maybe a sample code where we could see the usage of
> it. I`ve something about DI but still I have problems with proper usage
> in extbase.
>
> I would be very glad - and I think not only me - if I had opportunity to
> see DI in extbase in action

I already gave a example in this topic, although without description. 
Let me quote myself:

> /**
>  * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
>  */
> protected $configurationManager;
>
> /**
>  * Injects the Configuration Manager and is initializing the framework settings
>  *
>  * @param Tx_Extbase_Configuration_ConfigurationManagerInterface An instance of the Configuration Manager
>  * @return void
>  */
> public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
>     $this->configurationManager = $configurationManager;
>     $this->settings = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'ExtKeyInUpperCamelCase', 'PluginKeyInUpperCamelCase');
> }


For DI you only need to add a method starting with "inject" to your 
class, add proper annotation and type hinting in the method argument - 
that's it. To make use of DI you have to instantiate the class using the 
objectManager, something like $this->objectManager->get('classname'). 
(there is also a objectManager->create method that should be used for 
prototype classes AFAIK)

How you name the method after the "inject" prefix is up to you - it 
doesn't necessarily be the name of the property filled by it, but it's 
the most obvious to do.

So if you like to get one of your services injected, just create a 
injectYourService method and you're done.


/**
  * Dependency Injection for whatever you like
  *
  * @param Tx_Extkey_Service_FooBarService The service you like to get 
injected
  * @return void
  */
public function injectFooBarService( Tx_Extkey_Service_FooBarService 
$service) {
	// do with the $service variable whatever you like
	// use it directly do to some preprocessing
	// or simply assign it to a property for use in other methods
	$this->fooBarService = $service;
}

public function someFancyMethod($whatever) {
	return $this->fooBarService->doSomething($whatever);
}


The benefit of DI is, that you can get injected almost any class you 
need or only define that you need a object providing a certain interface 
without knowing which implementation was configured somewhere else. So 
if you f.e. like to send a mail and need a mailer object, you could 
simply ask for any object implementing a certain mailerInterface you can 
interact with. You don't care if it's a full blown mailing system using 
SMTP sockets or just a simple class using the php mail method - you only 
want to send a mail at this point and need a object that can do this 
supporting a certain interface. Which mailer to use all over your 
website might be configured somewhere else - out of the scope of your 
current application.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list