-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 2.3: [#5373] Small tweak per Stof's comment Fixed a minor grammar issue Fixed typos Link to the official repository of the bundle. Added mentions to some popular (and useful) Symfony bundles [#5095] Fixing a typo and updating to a more realistic example [#4228] Move synthetic services to its own recipe clarify bundle installation instructions Implemented the suggestions made by Christian and Wouter Replace phpDocumentor by the standard PHPDoc Implemented the changes suggested by reviewers Fixed an internal link reference Reviewed the Bundles cookbook articles Constraints - empty strings and null values Add a caution to the getUploadRootDir - correction Adding a caution to the getUploadRootDir() method [#4228] Moved requiring files to definitions
- Loading branch information
Showing
16 changed files
with
190 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
types | ||
parameters | ||
definitions | ||
synthetic_services | ||
compilation | ||
tags | ||
factories | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
.. index:: | ||
single: DependencyInjection; Synthetic Services | ||
|
||
How to Inject Instances into the Container | ||
------------------------------------------ | ||
|
||
When using the container in your application, you sometimes need to inject an | ||
instance instead of configuring the container to create a new instance. | ||
|
||
For instance, if you're using the :doc:`HttpKernel </components/http_kernel/introduction>` | ||
component with the DependencyInjection component, then the ``kernel`` | ||
service is injected into the container from within the ``Kernel`` class:: | ||
|
||
// ... | ||
abstract class Kernel implements KernelInterface, TerminableInterface | ||
{ | ||
// ... | ||
protected function initializeContainer() | ||
{ | ||
// ... | ||
$this->container->set('kernel', $this); | ||
|
||
// ... | ||
} | ||
} | ||
|
||
The ``kernel`` service is called a synthetic service. This service has to be | ||
configured in the container, so the container knows the service does exist | ||
during compilation (otherwise, services depending on this ``kernel`` service | ||
will get a "service does not exists" error). | ||
|
||
In order to do so, you have to use | ||
:method:`Definition::setSynthetic() <Symfony\\Component\\DependencyInjection\\Definition::setSynthetic>`:: | ||
|
||
use Symfony\Component\DependencyInjectino\Definition; | ||
|
||
// synthetic services don't specify a class | ||
$kernelDefinition = new Definition(); | ||
$kernelDefinition->setSynthetic(true); | ||
|
||
$container->setDefinition('your_service', $kernelDefinition); | ||
|
||
Now, you can inject the instance in the container using | ||
:method:`Container::set() <Symfony\\Component\\DependencyInjection\\Container::set>`:: | ||
|
||
$yourService = new YourObject(); | ||
$container->set('your_service', $yourService); | ||
|
||
``$container->get('your_service')`` will now return the same instance as | ||
``$yourService``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.