@@ -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
@@ -638,8 +638,10 @@ the env files ending in ``.local`` (``.env.local`` and ``.env.<environment>.loca
638
638
involving a ``.env.dist `` file. For information about upgrading, see:
639
639
:doc: `configuration/dot-env-changes `.
640
640
641
- Accessing Configuration Values
642
- ------------------------------
641
+ .. _configuration-accessing-parameters :
642
+
643
+ Accessing Configuration Parameters
644
+ ----------------------------------
643
645
644
646
Controllers and services can access all the configuration parameters. This
645
647
includes both the :ref: `parameters defined by yourself <configuration-parameters >`
@@ -650,9 +652,31 @@ all the parameters that exist in your application:
650
652
651
653
$ php bin/console debug:container --parameters
652
654
653
- Parameters are injected in services as arguments to their constructors.
654
- :doc: `Service autowiring </service_container/autowiring >` doesn't work for
655
- parameters. Instead, inject them explicitly:
655
+ In controllers extending from the :ref: `AbstractController <the-base-controller-class-services >`,
656
+ use the ``getParameter() `` helper::
657
+
658
+ // src/Controller/UserController.php
659
+ namespace App\Controller;
660
+
661
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
662
+
663
+ class UserController extends AbstractController
664
+ {
665
+ // ...
666
+
667
+ public function index()
668
+ {
669
+ $projectDir = $this->getParameter('kernel.project_dir');
670
+ $adminEmail = $this->getParameter('app.admin_email');
671
+
672
+ // ...
673
+ }
674
+ }
675
+
676
+ In services and controllers not extending from ``AbstractController ``, inject
677
+ the parameters as arguments of their constructors. You must inject them
678
+ explicitly because :doc: `service autowiring </service_container/autowiring >`
679
+ doesn't work for parameters:
656
680
657
681
.. configuration-block ::
658
682
0 commit comments