Skip to content

Commit 7854460

Browse files
committed
minor #12333 Mention the getParameter() helper in the configuration article (javiereguiluz)
This PR was merged into the 4.3 branch. Discussion ---------- Mention the getParameter() helper in the configuration article Fixes #8996. Commits ------- d8e43b9 Mention the getParameter() helper in the configuration article
2 parents e561965 + d8e43b9 commit 7854460

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

configuration.rst

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ a new ``locale`` parameter is added to the ``config/services.yaml`` file).
320320

321321
.. seealso::
322322

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>`.
325325

326326
.. index::
327327
single: Environments; Introduction
@@ -642,8 +642,10 @@ the env files ending in ``.local`` (``.env.local`` and ``.env.<environment>.loca
642642
involving a ``.env.dist`` file. For information about upgrading, see:
643643
:doc:`configuration/dot-env-changes`.
644644

645-
Accessing Configuration Values
646-
------------------------------
645+
.. _configuration-accessing-parameters:
646+
647+
Accessing Configuration Parameters
648+
----------------------------------
647649

648650
Controllers and services can access all the configuration parameters. This
649651
includes both the :ref:`parameters defined by yourself <configuration-parameters>`
@@ -654,9 +656,31 @@ all the parameters that exist in your application:
654656
655657
$ php bin/console debug:container --parameters
656658
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:
660684

661685
.. configuration-block::
662686

0 commit comments

Comments
 (0)