38f03bd2f5
* 2.3: [FrameworkBundle] Fix title and placeholder rendering in php form templates. RequestDataCollector - small fix renamed composer.phar to composer to be consistent with the Symfony docs [FrameworkBundle] bumped min version of Routing to 2.3 removed composer --dev option everywhere fixed a test [Console] Fixed output bug, if escaped string in a formatted string. [Security] Remove ContextListener's onKernelResponse listener as it is used Revert "minor #12652 [HttpFoundation] [Hackday] #9942 test: Request::getContent() for null value (skler)" Revert "fixed assertion" fixed assertion [HttpFoundation] [Hackday] #9942 test: Request::getContent() for null value fixed URL Add reference to documentation in FormEvents phpdocs [YAML] Fix one-liners to work with multiple new lines Keep "pre" meaning for var_dump quick-and-dirty debug [Console][Table] Fix cell padding with multi-byte Conflicts: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php src/Symfony/Bundle/FrameworkBundle/composer.json src/Symfony/Component/Console/Helper/TableHelper.php
1.6 KiB
1.6 KiB
DependencyInjection Component
DependencyInjection manages your services via a robust and flexible Dependency Injection Container.
Here is a simple example that shows how to register services and parameters:
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
$sc = new ContainerBuilder();
$sc
->register('foo', '%foo.class%')
->addArgument(new Reference('bar'))
;
$sc->setParameter('foo.class', 'Foo');
$sc->get('foo');
Method Calls (Setter Injection):
$sc = new ContainerBuilder();
$sc
->register('bar', '%bar.class%')
->addMethodCall('setFoo', array(new Reference('foo')))
;
$sc->setParameter('bar.class', 'Bar');
$sc->get('bar');
Factory Class:
If your service is retrieved by calling a static method:
$sc = new ContainerBuilder();
$sc
->register('bar', '%bar.class%')
->setFactory(array('%bar.class%', 'getInstance'))
->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');
$sc->get('bar');
File Include:
For some services, especially those that are difficult or impossible to autoload, you may need the container to include a file before instantiating your class.
$sc = new ContainerBuilder();
$sc
->register('bar', '%bar.class%')
->setFile('/path/to/file')
->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');
$sc->get('bar');
Resources
You can run the unit tests with the following command:
$ cd path/to/Symfony/Component/DependencyInjection/
$ composer install
$ phpunit