Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minimal cookbook article about the shared flag #5922

Merged
merged 1 commit into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ As a bonus, the ``Mailer`` service is only created once and the same
instance is returned each time you ask for the service. This is almost always
the behavior you'll need (it's more flexible and powerful), but you'll learn
later how you can configure a service that has multiple instances in the
":doc:`/cookbook/service_container/scopes`" cookbook article.
":doc:`/cookbook/service_container/shared`" cookbook article.

.. note::

Expand Down
1 change: 1 addition & 0 deletions cookbook/service_container/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ Service Container
.. toctree::
:maxdepth: 2

shared
scopes
compiler_passes
5 changes: 5 additions & 0 deletions cookbook/service_container/scopes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ How to Work with Scopes

The "container scopes" concept explained in this article has been deprecated
in Symfony 2.8 and it will be removed in Symfony 3.0.

Use the ``request_stack`` service (introduced in Symfony 2.4) instead of
the ``request`` service/scope and use the ``shared`` setting (introduced in
Symfony 2.8) instead of the ``prototype`` scope
(:doc:`read more about shared services </cookbook/service_container/shared>`).

This article is all about scopes, a somewhat advanced topic related to the
:doc:`/book/service_container`. If you've ever gotten an error mentioning
Expand Down
43 changes: 43 additions & 0 deletions cookbook/service_container/shared.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.. index::
single: Service Container; Shared Services

How to Define Not Shared Services
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"not shared", "non shared", "unshared", ...? (in other words: I'm not sure how to call this)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO "non shared"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for "non shared"

=================================

.. versionadded:: 2.8
The ``shared`` setting was introduced in Symfony 2.8. Prior to Symfony 2.8,
you had to use the ``prototype`` scope.

In the service container, all services are shared by default. This means that
each time you retrieve the service, you'll get the *same* instance. This is
often the behaviour you want, but in some cases, you might want to always get a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

behavior

*new* instance.

In order to always get a new instance, set the ``shared`` setting to ``false``
in your service definition:

.. configuration-block::

.. code-block:: yaml
services:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing # app/config/services.yml

app.some_not_shared_service:
class: ...
shared: false
# ...
.. code-block:: xml
<services>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing # app/config/services.xml

<service id="app.some_not_shared_service" class="..." shared="false" />
</services>
.. code-block:: php
$definition = new Definition('...');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing # app/config/services.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and missing use statement

$definition->setShared(false);
$container->setDefinition('app.some_not_shared_service', $definition);
Now, whenever you call ``$container->get('app.some_not_shared_service')`` or
inject this service, you'll recieve a new instance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: recieve -> receive