@@ -320,8 +320,8 @@ a new ``locale`` parameter is added to the ``config/services.yaml`` file).
320
320
321
321
.. seealso ::
322
322
323
- Read the ` Accessing Configuration Values `_ section of this article to learn
324
- about how to use these configuration parameters in services and controllers .
323
+ Later in this article you can read how to
324
+ ref:` get configuration parameters in controllers and services <configuration-accessing-parameters> ` .
325
325
326
326
.. index ::
327
327
single: Environments; Introduction
@@ -642,8 +642,10 @@ the env files ending in ``.local`` (``.env.local`` and ``.env.<environment>.loca
642
642
involving a ``.env.dist `` file. For information about upgrading, see:
643
643
:doc: `configuration/dot-env-changes `.
644
644
645
- Accessing Configuration Values
646
- ------------------------------
645
+ .. _configuration-accessing-parameters :
646
+
647
+ Accessing Configuration Parameters
648
+ ----------------------------------
647
649
648
650
Controllers and services can access all the configuration parameters. This
649
651
includes both the :ref: `parameters defined by yourself <configuration-parameters >`
@@ -654,9 +656,31 @@ all the parameters that exist in your application:
654
656
655
657
$ php bin/console debug:container --parameters
656
658
657
- Parameters are injected in services as arguments to their constructors.
658
- :doc: `Service autowiring </service_container/autowiring >` doesn't work for
659
- parameters. Instead, inject them explicitly:
659
+ In controllers extending from the :ref: `AbstractController <the-base-controller-class-services >`,
660
+ use the ``getParameter() `` helper::
661
+
662
+ // src/Controller/UserController.php
663
+ namespace App\Controller;
664
+
665
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
666
+
667
+ class UserController extends AbstractController
668
+ {
669
+ // ...
670
+
671
+ public function index()
672
+ {
673
+ $projectDir = $this->getParameter('kernel.project_dir');
674
+ $adminEmail = $this->getParameter('app.admin_email');
675
+
676
+ // ...
677
+ }
678
+ }
679
+
680
+ In services and controllers not extending from ``AbstractController ``, inject
681
+ the parameters as arguments of their constructors. You must inject them
682
+ explicitly because :doc: `service autowiring </service_container/autowiring >`
683
+ doesn't work for parameters:
660
684
661
685
.. configuration-block ::
662
686
0 commit comments