Skip to content

Commit 6d01473

Browse files
committed
Updating more types, and basically rewriting the alias article
1 parent 1f7d4b5 commit 6d01473

File tree

8 files changed

+369
-286
lines changed

8 files changed

+369
-286
lines changed

security.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,6 @@ Authentication (Identifying/Logging in the User)
12891289
security/api_key_authentication
12901290
security/custom_authentication_provider
12911291
security/pre_authenticated
1292-
security/target_path
12931292
security/csrf_in_login_form
12941293
security/named_encoders
12951294
security/multiple_user_providers

security/access_denied_handler.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ configure it under your firewall:
4949
.. code-block:: xml
5050
5151
<config>
52-
<firewall name="main">
53-
<access_denied_handler>AppBundle\Security\AccessDeniedHandler</access_denied_handler>
54-
</firewall>
52+
<firewall name="main">
53+
<access_denied_handler>AppBundle\Security\AccessDeniedHandler</access_denied_handler>
54+
</firewall>
5555
</config>
5656
5757
.. code-block:: php

security/api_key_authentication.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ and ``provider`` keys:
338338
339339
$container->loadFromExtension('security', array(
340340
'providers' => array(
341-
'api_key_user_provider' => array(
341+
'api_key_user_provider' => array(
342342
'id' => ApiKeyUserProvider::class,
343343
),
344344
),
345345
'firewalls' => array(
346-
'secured_area' => array(
346+
'main' => array(
347347
'pattern' => '^/api',
348348
'stateless' => true,
349349
'simple_preauth' => array(
@@ -594,7 +594,7 @@ current URL is before creating the token in ``createToken()``::
594594
// set the only URL where we should look for auth information
595595
// and only return the token if we're at that URL
596596
$targetUrl = '/login/check';
597-
if ($request->getPathInfo() != $targetUrl)
597+
if ($request->getPathInfo() !== $targetUrl)
598598
return;
599599
}
600600

security/form_login.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ directly to the login page), then the user is redirected to the default page,
3131
which is ``/`` (i.e. the homepage) by default. You can change this behavior
3232
in several ways.
3333

34-
.. note::
35-
36-
As mentioned, by default the user is redirected back to the page originally
37-
requested. Sometimes, this can cause problems, like if a background Ajax
38-
request "appears" to be the last visited URL, causing the user to be
39-
redirected there. For information on controlling this behavior, see
40-
:doc:`/security/target_path`.
41-
4234
Changing the default Page
4335
~~~~~~~~~~~~~~~~~~~~~~~~~
4436

service_container.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,10 @@ You can override any service that's imported by using its id (class name) below
858858
the options (e.g. ``public``) are inherited from the import (but the overridden
859859
service *does* still inherit from ``_defaults``).
860860

861+
You can also ``exclude`` certain paths. This is optional, but will slightly increase
862+
performance in the ``dev`` environment: excluded paths are not tracked and so modifying
863+
them will not cause the container to be rebuilt.
864+
861865
.. note::
862866

863867
Wait, does this mean that *every* class in ``src/AppBundle`` is registered as

service_container/alias_private.rst

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,33 @@ How to Create Service Aliases and Mark Services as Private
99
Marking Services as Public / Private
1010
------------------------------------
1111

12-
When defining services, you'll usually want to be able to access these definitions
13-
within your application code. These services are called *public*. For
14-
example, the ``doctrine`` service is a public service. This means that you can
15-
fetch it from the container using the ``get()`` method::
12+
When defining a service, it can be made to be *public* or *private*. If a service
13+
is *public*, it means that you can access it directly from the container at runtime.
14+
For example, the ``doctrine`` service is a public service::
1615

16+
// only public services can be accessed in this way
1717
$doctrine = $container->get('doctrine');
1818

19-
In some cases, a service *only* exists to be injected into another service
20-
and is *not* intended to be fetched directly from the container as shown
21-
above.
19+
But typically, services are accessed using :ref:`dependency injection <services-constructor-injection>`.
20+
And in this case, those services do *not* need to be public.
2221

2322
.. _inlined-private-services:
2423

25-
In these cases, to get a minor performance boost and ensure the service will not
26-
be retrieved directly from the container, you can set the service to be *not*
27-
public (i.e. private):
24+
So unless you *specifically* need to access a service directly from the container
25+
via ``$container->get()``, the best-practice is to make your services *private*.
26+
In fact, the :ref:`default services.yml configuration <container-public>` configures
27+
all services to be private by default.
28+
29+
You can also control the ``public`` option on a service-by-service basis:
2830

2931
.. configuration-block::
3032

3133
.. code-block:: yaml
3234
3335
services:
34-
foo:
35-
class: Example\Foo
36+
# ...
37+
38+
AppBundle\Service\Foo:
3639
public: false
3740
3841
.. code-block:: xml
@@ -43,29 +46,30 @@ public (i.e. private):
4346
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4447
4548
<services>
46-
<service id="foo" class="Example\Foo" public="false" />
49+
<service id="AppBundle\Service\Foo" public="false" />
4750
</services>
4851
</container>
4952
5053
.. code-block:: php
5154
52-
use Example\Foo;
55+
use AppBundle\Service\Foo;
5356
54-
$container->register('foo', Foo::class)
57+
$container->register(Foo::class)
5558
->setPublic(false);
5659
57-
What makes private services special is that, since the container knows that the
58-
service will never be requested from outside, it can optimize whether and how it
59-
is instanciated. This increases the container's performance.
60+
Private services are special because they allow the container to optimize whether
61+
and how they are instantiated. This increases the container's performance.
6062

6163
Now that the service is private, you *should not* fetch the service directly
6264
from the container::
6365

64-
$container->get('foo');
66+
use AppBundle\Service\Foo;
67+
68+
$container->get(Foo::class);
6569

6670
This *may or may not work*, depending on how the container has optimized the
67-
service instanciation and, even in the cases where it works, is
68-
deprecated. Simply said: A service can be marked as private if you do not want
71+
service instantiation and, even in the cases where it works, is
72+
deprecated. Simply said: A service should be marked as private if you do not want
6973
to access it directly from your code.
7074

7175
However, if a service has been marked as private, you can still alias it
@@ -76,6 +80,8 @@ However, if a service has been marked as private, you can still alias it
7680
Services are by default public, but it's considered a good practice to mark
7781
as much services private as possible.
7882

83+
.. _services-alias:
84+
7985
Aliasing
8086
--------
8187

@@ -88,11 +94,13 @@ services.
8894
.. code-block:: yaml
8995
9096
services:
91-
app.phpmailer:
92-
class: AppBundle\Mail\PhpMailer
97+
# ...
98+
AppBundle\Mail\PhpMailer:
99+
public: false
93100
94101
app.mailer:
95-
alias: app.phpmailer
102+
alias: AppBundle\Mail\PhpMailer
103+
public: true
96104
97105
.. code-block:: xml
98106
@@ -102,22 +110,23 @@ services.
102110
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
103111
104112
<services>
105-
<service id="app.phpmailer" class="AppBundle\Mail\PhpMailer" />
113+
<service class="AppBundle\Mail\PhpMailer" public="false" />
106114
107-
<service id="app.mailer" alias="app.phpmailer" />
115+
<service id="app.mailer" alias="AppBundle\Mail\PhpMailer" />
108116
</services>
109117
</container>
110118
111119
.. code-block:: php
112120
113121
use AppBundle\Mail\PhpMailer;
114122
115-
$container->register('app.phpmailer', PhpMailer::class);
123+
$container->register(PhpMailer::class)
124+
->setPublic(false);
116125
117-
$container->setAlias('app.mailer', 'app.phpmailer');
126+
$container->setAlias('app.mailer', PhpMailer::class);
118127
119128
This means that when using the container directly, you can access the
120-
``app.phpmailer`` service by asking for the ``app.mailer`` service like this::
129+
``PhpMailer`` service by asking for the ``app.mailer`` service like this::
121130

122131
$container->get('app.mailer'); // Would return a PhpMailer instance
123132

@@ -141,8 +150,7 @@ or you decided not to maintain it anymore), you can deprecate its definition:
141150

142151
.. code-block:: yaml
143152
144-
acme.my_service:
145-
class: ...
153+
AppBundle\Service\OldService:
146154
deprecated: The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
147155
148156
.. code-block:: xml
@@ -153,16 +161,18 @@ or you decided not to maintain it anymore), you can deprecate its definition:
153161
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
154162
155163
<services>
156-
<service id="acme.my_service" class="...">
164+
<service id="AppBundle\Service\OldService">
157165
<deprecated>The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.</deprecated>
158166
</service>
159167
</services>
160168
</container>
161169
162170
.. code-block:: php
163171
172+
use AppBundle\Service\OldService;
173+
164174
$container
165-
->register('acme.my_service', '...')
175+
->register(OldService::class)
166176
->setDeprecated(
167177
true,
168178
'The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.'
@@ -189,6 +199,6 @@ occurrence of the ``%service_id%`` placeholder in your template.
189199
deprecated, until when it will be maintained and the alternative services
190200
to use (if any).
191201

192-
For service decorators (see above), if the definition does not modify the
193-
deprecated status, it will inherit the status from the definition that is
194-
decorated.
202+
For service decorators (see :doc:`/service_container/service_decoration`), if the
203+
definition does not modify the deprecated status, it will inherit the status from
204+
the definition that is decorated.

0 commit comments

Comments
 (0)